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.
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?
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?