Issue
for clarity i am using python3.10.9
The program i am trying to make consists of a ThreadPool and some type of async runtime environment. If a connection comes in on a listening socket, the async environment should be able to handle multiple connections concurrently. Database writing and other proccessing work I want to be able to offload to threads.
I have never used async in Python and per my understanding multithreading is hard but can be achieved with something like multiprocessing
module.
Can this type of application be built in Python? Help appreciated!
EDIT: I suppose for a little more context, I found several of these types of examples in other programming books, so if there is a similar python tutorial I may have overlooked please send it my way!
Solution
You know your question is to vague.
But probably what you want can be achieved with an asyncio loop, and dispatching what you want to run in other threads with the call asyncio.run_in_executor
- which can use a pool of thread-based workers, or even of off-process based workers to run code not written with asyncio in mind.
One other thing that can be said, is that multi-threading in Python is fine if your wrokers will handle I/O - because the libs used to access that will usually free Python's Global Interpreter Lock (GIL) when waiting for i/o responses - so other Python threads can move forward. But only a single thread can run actual Python intructions at a time, so if you are using a lot of logic, and computations without I/O in your program, you have to use multiprocessing, in order to make use of extra CPU cores and have your program actually run parts in parallel.
From the asyncio side you will use the same call: run_in_executor
- but you will want to create a ProcessPoolExecutor
, instead of using the default ThreadPoolExecutor
the call can spawn -
Answered By - jsbueno
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.