Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Multiprocessing vs Eventlet

Tags:

python

Based on my understanding, threads cannot be executed in parallel(executed based on availability and random) and thats the reason Eventlet are being used.

If Eventlets are more for parallelism why can't we just use multiprocessing module of Python.

I thought of executing multi process modules and use the join method() to check if all the process are complete.

Can someone explain if my understanding is correct?

like image 630
user1050619 Avatar asked Sep 28 '15 21:09

user1050619


People also ask

Is multiprocessing faster than multithreading?

Multiprocessing outshines threading in cases where the program is CPU intensive and doesn't have to do any IO or user interaction. For example, any program that just crunches numbers will see a massive speedup from multiprocessing; in fact, threading will probably slow it down.

Does Python use multiprocessing?

True parallelism in Python is achieved by creating multiple processes, each having a Python interpreter with its own separate GIL. Python has three modules for concurrency: multiprocessing , threading , and asyncio . When the tasks are CPU intensive, we should consider the multiprocessing module.

How do I share data between two processes in Python?

Passing Messages to Processes A simple way to communicate between process with multiprocessing is to use a Queue to pass messages back and forth. Any pickle-able object can pass through a Queue. This short example only passes a single message to a single worker, then the main process waits for the worker to finish.

What is multithreading and multiprocessing in Python?

What's the difference between Python threading and multiprocessing? With threading, concurrency is achieved using multiple threads, but due to the GIL only one thread can be running at a time. In multiprocessing, the original process is forked process into multiple child processes bypassing the GIL.


1 Answers

Based on my understanding, threads cannot be executed in parallel (executed based on availability and random)

Correct

and thats the reason Eventlet are being used.

Not so correct. The Eventlet library is used to simplify non-blocking IO programming. It does not actually add parallelism. Thread execution is still limited to one thread at a time due to the GIL. But it is used because it greatly simplifies the process of launching, scheduling, and managing IO-bound threads, particularly ones that do not need to interact with each other.

If Eventlets are more for parallelism

As I just mentioned, this is not what they exist for.

why can't we just use multiprocessing module of Python. I thought of executing multi process modules and use the join method() to check if all the process are complete.

You certainly can! And you will get actual parallel execution with this approach. But you may not get the same speedup. The multiprocessing library is better suited for CPU-bound parallel tasks, because those are the ones that need more frequent access to the interpreter. You may actually see an increase in execution time when using multiprocessing with IO-bound tasks because of the overhead of multiple process execution and management.


As is the case with most optimization and execution time questions, trying both and profiling is the surefire way to guarantee you're using the "best" option for your application. Though you may find that if you write the code to utilize Eventlets first, then try to modify it to use regular threads or multiprocessing, you'll have to write more boilerplate code just to manage the threads or processes, and the value of Eventlets should become more obvious.

like image 178
skrrgwasme Avatar answered Sep 27 '22 22:09

skrrgwasme