I wrote a small program to evaluate the state of cellular automata and paint it on the screen. I have dual-core PC, so in order to increase efficiency I decided to make calculations in two threads. I made my own implementations of QThread, like so:
clacc MyThread(QThread):
def run(self):
...
Then I’m making two instances of this class and calling them:
thread1 = MyThread()
thread2 = MyThread()
thread1.start()
thread2.start()
When I opened the Task Manager, I found that they’re executing one after another: thread1 first, then thread2, then thread1 again and so on.
Question is: is there a way to invoke them so they will work independently, on different cores and in one moment of time, not one after another?
If you use threads, you can’t increase efficiency as you wish, because it won’t do paarallelization. You need to do multiprocessing. To do that, you can use the mulltiprocessing module, which works like threading module :
You won’t share the same context so you can’t communicate directly by methods calls. I advise you to use some “message queue” system like : multiprocessing.Queue or zeromq (great great lib !).
If you look at “Ventillator” zmq design pattern you’ll see it do exactly what you need : paralellize workers.