Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python 3.6 and ValueError: loop argument must agree with Future

I just want to run a simple test example yet I get the below error. How do I resolve?

import asyncio
import uvloop
import concurrent.futures
import time
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

async def do_some_work(x):

    while True:
        print("Waiting " + str(x))
        await asyncio.sleep(x)

if __name__ == '__main__': 


    loop = asyncio.new_event_loop()
    tasks = [asyncio.ensure_future(do_some_work(2)), 
             asyncio.ensure_future(do_some_work(5))]
    loop.run_until_complete(asyncio.gather(*tasks))

Traceback (most recent call last):
  File "/Users/worker_why.py", line 23, in <module>
    loop.run_until_complete(asyncio.gather(*tasks))
  File "uvloop/loop.pyx", line 1181, in uvloop.loop.Loop.run_until_complete (uvloop/loop.c:25184)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py", line 508, in ensure_future
    raise ValueError('loop argument must agree with Future')
ValueError: loop argument must agree with Future
like image 378
Tampa Avatar asked Oct 18 '17 08:10

Tampa


1 Answers

loop = asyncio.new_event_loop()

If you create new event loop and don't want to pass it everywhere, you should also make this loop current for context:

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
like image 166
Mikhail Gerasimov Avatar answered Oct 23 '22 23:10

Mikhail Gerasimov