See the comment posted by @juanpa.arrivillaga. Just specify as the target argument of the Process
constructor the name of the function; do not call the function. The arguments to your target are specified separately as the args argument, either as a tuple
or a list
.
Also, since f
is not returning a useful value, there is no reason to have Queue
instances for the results. In any case, you are not passing the Queue
instances to f
and nothing is being put
to the queue so I can't understand why you would be attempting to issue calls to get
against these Queue
instances; these calls will hang forever. The argument you do need to be passing to f
is the name, as follows:
from multiprocessing import Process
#from time import sleep
import time
def f(name):
print('hello', name)
time.sleep(10)
start_time = time.time()
a = Process(target=f, args=('Tom',))
a.start()
b = Process(target=f, args=("Stock",))
b.start()
# Wait for processes to complete
a.join()
b.join()
elapsed_time = time.time() - start_time
print(elapsed_time)
Prints:
hello Tom
hello Stock
10.212252855300903
If you were running this on a platform such as Windows, then you would need to place the process-creating code in a special block as follows;
from multiprocessing import Process
#from time import sleep
import time
def f(name):
print('hello', name)
time.sleep(10)
# Required on platforms that create processes with the "spawn" method:
if __name__ == '__main__':
start_time = time.time()
a = Process(target=f, args=('Tom',))
a.start()
b = Process(target=f, args=("Stock",))
b.start()
# Wait for processes to complete
a.join()
b.join()
elapsed_time = time.time() - start_time
print(elapsed_time)