-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathmain_demo.py
38 lines (31 loc) · 1.21 KB
/
main_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import asyncio
from httpx import AsyncClient
from arq import create_pool
from arq.connections import RedisSettings
# Here you can configure the Redis connection.
# The default is to connect to localhost:6379, no password.
REDIS_SETTINGS = RedisSettings()
async def download_content(ctx, url):
session: AsyncClient = ctx['session']
response = await session.get(url)
print(f'{url}: {response.text:.80}...')
return len(response.text)
async def startup(ctx):
ctx['session'] = AsyncClient()
async def shutdown(ctx):
await ctx['session'].aclose()
async def main():
redis = await create_pool(REDIS_SETTINGS)
for url in ('https://facebook.com', 'https://microsoft.com', 'https://github.com'):
await redis.enqueue_job('download_content', url)
# WorkerSettings defines the settings to use when creating the work,
# It's used by the arq CLI.
# redis_settings might be omitted here if using the default settings
# For a list of all available settings, see https://arq-docs.helpmanual.io/#arq.worker.Worker
class WorkerSettings:
functions = [download_content]
on_startup = startup
on_shutdown = shutdown
redis_settings = REDIS_SETTINGS
if __name__ == '__main__':
asyncio.run(main())