Python threading / fork?

I am making a python script that should do 3 things at a time. What is a good way to achieve this, just like what I heard about GIL, I'm not so inclined to use threads anymore. 2 of the things that the script needs to execute will be very active, they will have a lot of work, and then I need the third message to be sent to the user through the socket when he asks (this will be a tiny server) about the status of the other 2 processes.
Now my question is, what would be a good way to achieve this? I do not want to have three different scripts, and also because GIL uses threads. I think I will not get more performance, and I will make things worse. Is there fork () for python, like in C, so from my processes script fork 2 that will do their job and tell the user from the main process? And how can I communicate with forked processes using the main process?

LE:: more precisely, 1thread should receive emails from the imap server and store them in the database, another stream should receive messages from db that should be sent, and then send them and the main stream should be a tiny http server that just accepts one URL and will show the status of these two streams in json format. Also have oK themes? will the work be done simultaneously or due to gil, will there be performance problems?

+4
source share
2 answers

I think you could use the multiprocessing package, which has an API similar to a streaming package, and allows you to get better performance with multiple cores on the same processor.

To view performance gains using multiprocessing rather than threads, check out this link about average time comparisons of the same program using multiprocessing x threading.

+3
source

GIL really only cares if you want to perform multiprocessing, which distributes the load across multiple cores / processors. If so, and it looks like the description from your description, use multiprocessing.

If you just need to do three things β€œat the same time” in such a way that you need to wait in the background for something to happen, then the threads are just fine. What are topics for? 8th)

+2
source

Source: https://habr.com/ru/post/1299192/


All Articles