Reputation: 1
I've been reading up a lot about python processing, but I can't seem to figure out how I can run two methods/processes at the same time. Here is my code, it is in a file named supertest.py, which I am running from the console:
import threading
import time
import multiprocessing
def printFive():
for i in range(5):
print 'printFiveCalled'
time.sleep(1)
def printTen():
for i in range(10):
print 'printTenCalled'
time.sleep(0.5)
if __name__ == "__main__":
p1 = multiprocessing.Process(name = 'p1', target = printFive())
p2 = multiprocessing.Process(name = 'p2', target = printTen())
p1.start()
p2.start()
I've tried running threads, processes, everything, but for some reason, all I get in the output is this:
printFiveCalled
printFiveCalled
printFiveCalled
printFiveCalled
printFiveCalled
printTenCalled
printTenCalled
printTenCalled
printTenCalled
printTenCalled
printTenCalled
printTenCalled
printTenCalled
printTenCalled
printTenCalled
I want something like this to print on the console
printFiveCalled
printTenCalled
printTenCalled
printFiveCalled
printTenCalled
printTenCalled
Upvotes: 0
Views: 945
Reputation: 5746
Close! I think the issue comes from the brackets in your target
:
import time
import multiprocessing as mp
import os
def printFive():
for i in range(5):
print ('printFiveCalled')
time.sleep(1)
def printTen():
for i in range(10):
print ('printTenCalled')
time.sleep(0.5)
if __name__ == "__main__":
p1 = mp.Process(target = printFive)
p2 = mp.Process(target = printTen)
p1.start()
p2.start()
p1.join()
p2.join()
os.system("pause")
Then the difference between threading and multiprocessing. With multiprocessing, you are going to use several cores of your computer at the same time (1 / process is ideal). However, python threading is not real multithreading because of the global interpreter lock which will forbid the run of several threads at the same time.
Conclusion: The threads are run serially, the processes are run in parallel. Threads are useful when part of the computation is not done in Python. i.e. when using numpy, OpenCV, ... libraries which swap to C.
Last point for the multiprocessing: the easiest way to use it is to create 1 function which does a lot of computation and which need to run on multiple inputs. Then, a Pool()
and a map()
can be used.
if __name__ == "__main__":
with mp.Pool(processes = N) as p:
# Function with 1 argument:
p.map(f, inputs)
# Function with multiple arguments:
p.starmap(f, [(arg1, arg2) for arg1 in input1 for arg2 in input2])
NB: For threading, a way I appreciate is using multiprocessing.dummy
which works exactly as multiprocessing
but with threads.
Upvotes: 1
Reputation: 1228
The first thing I notice is that the target is supposed to be a function, which means you pass the function (target=foo
) instead of passing what the function returns (target=foo()
).
Using the threading interface, it is easy to create two treads each running one of your functions, and get the output you'd expect.
import threading
import time
def printFive():
for i in range(5):
print('5\n', end='', flush=True)
time.sleep(1)
def printTen():
for i in range(10):
print('1\n', end='', flush=True)
time.sleep(0.5)
if __name__ == "__main__":
t0 = threading.Thread(target = printFive)
t1 = threading.Thread(target = printTen)
t0.start()
t1.start()
Outputs:
5
1
1
5
1
1
5
1
1
5
1
1
5
1
1
Furthermore, if you would like to pass arguments to the target you use the args
and kwargs
keywords.
e.g. threading.Thread(target=print_some_num_some_times, args=(5,), kwargs={'times': 10})
Upvotes: 0