Hello!
I’m making a chatbot based on Python 3.7.5 and AsyncIO on Linux. In order to add a REST-like interface to it, I want to use Sanic. The chatbot is plugin-based, which means the Sanic code I’ll be writing for it is in a file which is loaded using importlib after the chatbot has been started (this in itself is not a problem).
Since I already have working code which starts an AsyncIO loop, I predictably get an error if I try to use the first Sanic code example from the documentation as a plugin in my chatbot software:
RuntimeError: Cannot run the event loop while another loop is running
Therefore, I decided to try to instead use the example from https://sanic.readthedocs.io/en/latest/sanic/asyncio_python37.html, since it says:
This example shows how to use sanic with Python 3.7, to be precise: how to retrieve an asyncio server instance
This seems to be what I need (please tell me if I’m wrong).
So, without involving my chatbot at all for starters, I tried to get the example on that page running, but I failed to do so. First, it complains about /tmp/sanic.sock
not existing; that I can fix by adding:
except FilenotfoundError:
pass
But then, I get a trickier error:
$ python webhook2.py
Traceback (most recent call last):
File “webhook2.py”, line 37, in
loop.run_until_complete(srv.start_serving())
AttributeError: ‘AsyncioServer’ object has no attribute ‘start_serving’
So, basically, I have two questions:
- Am I barking up the wrong tree with trying to get this example to run? The problem I’m hoping it will solve for me, is getting Sanic to run when non-Sanic code has previously already started an AsyncIO loop.
- If I am barking up the right tree, then what’s wrong with the code in the example at https://sanic.readthedocs.io/en/latest/sanic/asyncio_python37.html? My code is copy-and-pasted from that example, with the addition of the “except FilenotfoundError” part I talked about above.