You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.1 KiB
Python

7 years ago
import asyncio
async def find_divisibles(inrange, div_by):
"""Really this is a bulk task"""
print("finding nums in range {} divisible by {}".format(inrange, div_by))
located = []
for i in range(inrange):
if i % div_by == 0:
located.append(i)
if i % 50000 == 0:
await asyncio.sleep(0.001)
print("Done w/ nums in range {} divisble by {}".format(inrange, div_by))
return located
async def main():
divs1 = loop.create_task(find_divisibles(50800000, 34113))
divs2 = loop.create_task(find_divisibles(100052, 3210))
divs3 = loop.create_task(find_divisibles(500, 3))
await asyncio.wait([divs1, divs2, divs3])
return divs1, divs2, divs3
if __name__ == '__main__':
try:
loop = asyncio.get_event_loop()
# returns a Task object, not the result objects themselves. You have to access the Task result
# to see the output
d1, d2, d3 = loop.run_until_complete(main())
print(type(d1))
print(d1.result())
except Exception as e:
pass
finally:
loop.close()