我正在调查this article并在我的本地进行相同的测试:
import os
import time
import threading
import multiprocessing
NUM_WORKERS = 4
def only_sleep():
print "PID: %s, Process Name: %s, Thread Name: %s" % (os.getpid(), multiprocessing.current_process(), threading.current_thread())
time.sleep(2)
def crunch_numbers():
print "PID: %s, Process Name: %s, Thread Name: %s" % (os.getpid(), multiprocessing.current_process(), threading.current_thread())
x = 0
while x < 10000000:
x += 1
def main():
start_time = time.time()
for _ in range(NUM_WORKERS):
only_sleep()
end_time = time.time()
print "serial time", end_time - start_time
start_time = time.time()
threads = [threading.Thread(target=only_sleep()) for _ in range(NUM_WORKERS)]
[thread.start() for thread in threads]
[thread.join() for thread in threads]
end_time = time.time()
print "Threads time = ", end_time - start_time
start_time = time.time()
processes = [multiprocessing.Process(target=only_sleep()) for _ in range(NUM_WORKERS)]
[process.start() for process in processes]
[process.join() for process in processes]
end_time = time.time()
print "Process time = ", end_time - start_time
if __name__ == '__main__':
main()
这是输出:
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
serial time 8.01504993439
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
Threads time = 8.01574707031
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
Process time = 8.0206348896
以下是我的本地系统配置:
所有三个串行、多线程、多进程都花费相同的时间来运行。有人可以帮助我理解为什么会发生这种情况吗?
最佳答案
当你引用一个函数时,你不应该调用它:
threading.Thread(target=only_sleep())
应该是:
threading.Thread(target=only_sleep)
还有:
processes = [multiprocessing.Process(target=only_sleep()) for _ in range(NUM_WORKERS)]
应该是:
processes = [multiprocessing.Process(target=only_sleep) for _ in range(NUM_WORKERS)]
()
用于调用函数。
关于Python - 串行进程、多线程、多进程都需要相同的时间在我的本地运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58578753/