开发者

Why thread is slower than subprocess ? when should I use subprocess in place of thread and vise versa

开发者 https://www.devze.com 2023-03-14 13:18 出处:网络
In my application, I have tried python threading and subprocess module to open firefox, and I have开发者_开发百科 noticed that subprocess is faster than threading. what could be the reason behind this

In my application, I have tried python threading and subprocess module to open firefox, and I have开发者_开发百科 noticed that subprocess is faster than threading. what could be the reason behind this?

when to use them in place of each other?


Python (or rather CPython, the c-based implementation that is commonly used) has a Global Intepreter Lock (a.k.a. the GIL).

Some kind of locking is necessary to synchronize memory access when several threads are accessing the same memory, which is what happens inside a process. Memory is not shared by between processes (unless you specifically allocate such memory), so no lock is needed there.

The globalness of the lock prevents several threads from running python code in the same process. When running mulitiple processes, the GIL does not interfere.

So, Python code does not scale on threads, you need processes for that.

Now, had your Python code mostly been calling C-APIs (NumPy/OpenGL/etc), there would be scaling since the GIL is usually released when native code is executing, so it's alright (and actually a good idea) to use Python to manage several threads that mostly execute native code.

(There are other Python interpreter implementations out there that do scale across threads (like Jython, IronPython, etc) but these aren't really mainstream.. yet, and usually a bit slower than CPython in single-thread scenarios.)

0

精彩评论

暂无评论...
验证码 换一张
取 消