Sanic: Sanic 0.3.1 Error of got Future attached to a different loop while normal with 0.1.9

Created on 15 Feb 2017  路  4Comments  路  Source: sanic-org/sanic

Config.REQUEST_TIMEOUT = 100000
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
app = Sanic(__name__)
loop = asyncio.get_event_loop()

@app.route("/")
async def voice(request):
        waittime = int(request.args.get('t',6))

        try:
           res = await asyncio.wait_for(queue.get(),timeout=waittime)
           if res is not None:
              res = res.decode().rstrip()
        except asyncio.TimeoutError:
           res = 'err'

        return text(res)

if __name__ == "__main__":
        queue = asyncio.Queue()
        loop.run_until_complete(connect(loop,queue))
        app.run(host="127.0.0.1", port=8001,debug=True)

And I got the error like this:
RuntimeError: Task cb=[_release_waiter()() at /usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py:350] created at /usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py:379> got Future attached to a different loop while handling uri /

And The same code indeed work normal with Sanic 0.1.9.

Anyone can help me ?

Thanks in advance.


Traceback (most recent call last):
File /usr/local/lib/python3.5/site-packages/sanic/sanic.py, line 329, in handle_request
response = await response
File /usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/coroutines.py, line 105, in __next__
return self.gen.send(None)
File voice.py, line 42, in voice
res = await asyncio.wait_for(queue.get(),timeout=waittime)
File /usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py, line 392, in wait_for
return fut.result()
File uvloop/future.pyx, line 146, in uvloop.loop.BaseFuture.result (uvloop/loop.c:102500)
File uvloop/future.pyx, line 101, in uvloop.loop.BaseFuture._result_impl (uvloop/loop.c:102039)
File uvloop/future.pyx, line 374, in uvloop.loop.BaseTask._fast_step (uvloop/loop.c:105596)
File /usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/queues.py, line 168, in get
yield from getter
File /usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py, line 361, in __iter__
yield self # This tells Task to wait for completion.

question

Most helpful comment

https://github.com/channelcat/sanic/blob/master/examples/cache_example.py is no longer a valid link, where can I find an example?

All 4 comments

In Sanic 0.3.1 or 0.3.0 there were some changes regarding loop policy. Try using the before_start signals.

You have an example in https://github.com/channelcat/sanic/blob/master/examples/cache_example.py

It's not a valid scenario to have 2 event loops (Yours and Sanic's) running at the same time. Please refer to @argaen's advice for instantiating anything with Sanic's loop.

Thanks very much.
Problem solved.

https://github.com/channelcat/sanic/blob/master/examples/cache_example.py is no longer a valid link, where can I find an example?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eseglem picture eseglem  路  4Comments

gsmcwhirter picture gsmcwhirter  路  4Comments

aiurlano picture aiurlano  路  4Comments

1067511899 picture 1067511899  路  3Comments

litelife picture litelife  路  3Comments