我一直在试验我的代码以发送“并行”命令到多个串行COM端口。Python多重处理比Windows上的多线程稍微慢
我的多线程代码包括:
global q
q = Queue()
devices = [0, 1, 2, 3]
for i in devices:
q.put(i)
cpus=cpu_count() #detect number of cores
logging.debug("Creating %d threads" % cpus)
for i in range(cpus):
t = Thread(name= 'DeviceThread_'+str(i), target=testFunc1)
t.daemon = True
t.start()
和多处理代码包括:
devices = [0, 1, 2, 3]
cpus=cpu_count() #detect number of cores
pool = Pool(cpus)
results = pool.map(multi_run_wrapper, devices)
我观察到在“并行发送串行命令至4个COM端口的任务“大约需要6秒钟,多次处理总是需要0.5到1秒的额外总运行时间。
关于为什么Windows机器上的差异的任何输入?
多处理总是有额外的开销与多线程。多线程使用与父进程相同的内存空间,而多进程需要为进程分配新的内存。游戏中可能有更多的因素,但这种开销是给定的。 –