此处讨论multiprocessing
与threading
的利弊:Multiprocessing vs Threading Python
我希望使用multiprocessing
在后台执行任务,同时继续执行程序的其余部分。使用threading
:background function in Python
import threading
def function_A(some_args):
# some stuff
def function_B(some_args):
#do some stuff
my_thread = threading.Thread(target=function_A, args=my_args)
my_thread.start()
#continue doing stuff
我尝试使用multiprocessing
:
import multiprocessing as mp
def function_A(some_args):
# some stuff
worker_pool = mp.Pool(processes=4)
def function_B(some_args, worker_pool):
# do some stuff
worker_pool.apply_async(function_A, args=my_args)
# continue doing stuff
我在测试脚本中尝试了这个:
import threading
import multiprocessing
import time
def function_A(task_description, sleep_time):
time.sleep(sleep_time)
print "function_A with task {} completed!\n".format(task_description)
def function_threading(sleep_time):
st = time.time()
for x in xrange(4):
print "Calling A and continuing (threading)..."
print "Time between calls: ", round(time.time() - st, 3)
a_thread = threading.Thread(target=function_A, args=("threading {}".format(x), sleep_time,))
a_thread.start()
st = time.time()
def function_multiprocessing(sleep_time, worker_pool):
st = time.time()
for x in xrange(4):
print "Calling A and continuing (multiprocessing)..."
print "Time between calls: ", round(time.time() - st, 3)
worker_pool.apply_async(function_A, args=("multiprocessing {}".format(x), sleep_time,))
st = time.time()
sleep_time = 2.5
# if __name__ == "__main__" statement required for multiprocessing on Windows
if __name__ == "__main__":
print "================================"
print "Threading test."
function_threading(sleep_time)
print "================================"
print "Multiprocessing test."
worker_pool = multiprocessing.Pool(processes=4)
function_multiprocessing(sleep_time, worker_pool)
print "================================"
这是我得到的输出:
================================
Threading test.
Calling A and continuing (threading)...
Time between calls: 0.0
Calling A and continuing (threading)...
Time between calls: 0.0
Calling A and continuing (threading)...
Time between calls: 0.0
Calling A and continuing (threading)...
Time between calls: 0.001
================================
Multiprocessing test.
Calling A and continuing (multiprocessing)...
Time between calls: 0.0
Calling A and continuing (multiprocessing)...
Time between calls: 0.001
Calling A and continuing (multiprocessing)...
Time between calls: 0.0
Calling A and continuing (multiprocessing)...
Time between calls: 0.0
================================
function_A with task threading 0 completed!
function_A with task threading 2 completed!
function_A with task threading 1 completed!
function_A with task threading 3 completed!
观察:
即使主要进程被杀死之后仍然存在子进程 - 这是来自线程还是多处理?我必须按CTRL + ALT + DELETE并手动终止它们
多处理任务永不打印
需要修复哪些脚本才能正常工作?或者,换句话说,如何使用multiprocessing
运行和管理后台进程?