I want to know why this example doesn't work asynchronously ? and what I should change that to make it work. I will really appreciate for any help !
Example 1:
import asyncio
import random
import time
nums = list()
[nums.append(x) for x in range(1, 100)]
random_time = random.randint(1, 5)
async def func(num):
num = num * 2
time.sleep(random_time)
print(num)
return num
async def main():
tasks = list()
for num in nums:
tasks.append(func(num))
return await asyncio.gather(*tasks)
loop = asyncio.get_event_loop()
results = loop.run_until_complete(main())
loop.close()
print(f'{len(results)} are asynchronized')
Example 2: