I need to run a quick experiment with python.
However, I am stuck with an async issue. I am not very familiar with python async programming, so please bear with me.
What I want is to run a script which starts different daemons at different ports, and then waits for connections to be established to it.
I used basically the code below (which does not run, because I wanted to simplify for the question, but it should be enough to make the case. If not, please let me know).
The issue I have is that this code (probably obvious to the experts) stalls at the first sleep_forever
. In other words, only one daemon gets started.
Can this be done at all from the same script? Or do I need subprocesses (or something else completely) to start?
I tried removing the await
from self.__start()
, but that results in an error saying __start() was not awaited
. My thinking was that then the async function would indeed not be awaited and the script would move on, while the network stuff would be initiated and then it would wait. Looks like it doesn’t work like that. I also tried starting a task, but then the task needs to be awaited as well?
import trio
class Daemon:
port: int
@classmethod
async def new(cls, port):
self = cls()
self.port = port
#assume this to return an `AbstractAsyncContextManager`
self.server = create_server() `
await self.__start()
async def __start(self):
print("starting with port...".format(port=self.port))
# init network socket
async with self.server.start(port=self.port), trio.open_nursery() as nursery:
#setup network objects
print("server ok")
# ->> WAIT FOR INCOMING Connections
await trio.sleep_forever()
print("exiting.")
async def run():
d_list = []
num = 3
for i in range(num):
d = await Daemon.new(5000+i)
d_list.append(d)
if __name__ == "__main__":
trio.run(run)