- https://docs.python.org/3/library/threading.html
- https://docs.python.org/3/library/multiprocessing.html
- Python: what are the differences between the threading and multiprocessing modules?
Using Python 3.4 on Linux
I’m new to parallel programming and I’m encountering problems when running the threading.Threads() for a specific method, and the module multiprocessing.Process() for another. Both methods work fine when the other one is commented out. Neither method has anything to do with the other (eg no attempt to share data). But when I have them both running neither works and everything freezes. As far as I can tell the multiprocessing seems to lock up. I assume the same thing applies for the Threading.
So the first step is to assert whether or not this is even possible?
(I have a feeling some of you will ask the reason for this... The threading does a simple capture user key checking while the multiprocessing does some heavy lifting)
I’m providing an example (more like pseudo code) to help illustrate how the methods are used.
file t.py
import threading
Class T:
Def __init__():
t = threading.Thread(target = self.ThreadMethod)
t.daemon = True
t.start()
Def ThreadMehod():
# capture key
file m.py
import multiproceessing
Class M:
Def __init__():
mp = multiprocessing.Process(target = self.ProcessMethod)
mp.start()
Def ProcessMethod():
# heavy lifting
file main.py
import T
import M
Class main:
Def __init__():
T()
Def DoTheProcess()
for i in range(5):
M()