-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
44 lines (33 loc) · 879 Bytes
/
example.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
39
40
41
42
43
44
import asyncio
import random
import asyncioworkers
async def coro(index):
for i in range(5):
await asyncio.sleep(1)
print("Task {}:".format(index), i)
return 0
async def producer(w):
tasks = []
for i in range(1000):
task = await w.run_coro(
coro(i),
priority=random.randint(asyncioworkers.CRITICAL_PRIORITY,
asyncioworkers.LOW_PRIORITY)
)
tasks.append(task)
for task in asyncio.as_completed(tasks):
await task
async def _main():
workers = asyncioworkers.Workers(100)
await workers.start()
await producer(workers)
await workers.stop()
del workers
def main():
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(_main())
finally:
loop.close()
if __name__ == '__main__':
main()