Я просматривал эту статью и тестировал то же самое в моем локальном:
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
Ниже мой локальныйконфигурация системы:
Все три последовательных, многопоточных, многопроцессорных выполняются одновременно. Может ли кто-нибудь помочь мне понять, почему это происходит?