Reputation: 9195
If I have 2 python processes, the first a script that runs an asyncio eventloop forever, and the second that is an HTTP API server, is there a way to access the event loop of the first process to add tasks to it from endpoints of the API server?
Like a user accesses /api/example
, and it would add a task, like using loop.call_soon()
to the event loop.
Is this possible when the 2 processes are executed completely separately?
Upvotes: 0
Views: 596
Reputation: 5630
This is not possible.
I could imagine (depending on what you want exactly and what these tasks are exactly) some kacks / work arounds:
One solution, that you could try is, that there is a connection between the two processes (e.g. a TCP connection, the asyncio process acting as TCP server, the HTTP server acting as client) where the HTTP server sends enough information over to the asyncio server to create the task and add it to its event loop.
Another option could be, that the http server creates a python file with the code to execute, notifies the asyncio server via a TCP connection or via a signal (e.g. signal HUP) to check in a given directory for a new file, import it and add it to its loop.
Security wise both solutions are a little tricky, as anybody being able to write a file in the specific directory or anybody being able to connect to the TCP socket might trigger an action.
But all depends on your context.
Or you could try to start the HTTP API server as a thread in the same process as your asyncio process?
Upvotes: 1