【发布时间】:2021-12-13 14:39:41
【问题描述】:
我尝试深入了解多处理。到目前为止一切顺利,我理解了这个概念。但是现在我想知道,为什么在使用多处理时我的打印语句没有显示出来。
有谁知道我的错误在哪里或者为什么 print 参数没有出现在多处理中?
这是我的代码和输出,没有多处理:
# -------------------------LINEAR PROCESSING--------------------------- #
import time
start = time.perf_counter()
def sleep(seconds):
print("Sleeping {} second(s) ...".format(seconds))
time.sleep(seconds)
print("Done Sleeping...")
# run sleep function 10 times
for _ in range(10): # _ throw away variable - hence not using integers of range
sleep(1.5)
finish = time.perf_counter()
print("Finished in {} second(s) without multi-processing".format(round(finish-start,2)))
# Output
Sleeping 1.5 second(s) ...
Done Sleeping...
Sleeping 1.5 second(s) ...
Done Sleeping...
Sleeping 1.5 second(s) ...
Done Sleeping...
Sleeping 1.5 second(s) ...
Done Sleeping...
Sleeping 1.5 second(s) ...
Done Sleeping...
Sleeping 1.5 second(s) ...
Done Sleeping...
Sleeping 1.5 second(s) ...
Done Sleeping...
Sleeping 1.5 second(s) ...
Done Sleeping...
Sleeping 1.5 second(s) ...
Done Sleeping...
Sleeping 1.5 second(s) ...
Done Sleeping...
Finished in 15.03 second(s) without multi-processing
这是我的代码和多处理输出:
# -------------------------MULTI-PROCESSING (OLD WAY)--------------------------- #
import multiprocessing
import time
start = time.perf_counter()
def sleep(seconds):
print("Sleeping {} second(s) ...".format(seconds))
time.sleep(seconds)
print("Done Sleeping...")
# create 10 processes for each sleep function and store it in list
processes = []
for _ in range(10): # _ throw away variable - hence not using integers of range
p = multiprocessing.Process(target=sleep, args=[1.5])
p.start()
processes.append(p)
# loop over started processes and wait until all processes are finished (join)
for process in processes:
process.join()
finish = time.perf_counter()
print("Finished in {} second(s) with multi-processing".format(round(finish-start,2)))
# Output
Finished in 0.14 second(s) with multi-processing
这是我的 jupyter notebook 统计数据:
jupyter core : 4.7.1
jupyter-notebook : 6.3.0
qtconsole : 5.0.3
ipython : 7.22.0
ipykernel : 5.3.4
jupyter client : 6.1.12
jupyter lab : 3.0.14
nbconvert : 6.0.7
ipywidgets : 7.6.3
nbformat : 5.1.3
traitlets : 5.0.5
【问题讨论】:
-
不是 100% 确定,但我认为每个进程都有自己的
stdout,而不是与主脚本相同的输出。要从不同的进程读取标准输出,您需要重定向或读取这些流。类似的问题stackoverflow.com/questions/30793624/… -
或者不使用打印,而是将字符串输出到队列,然后主脚本可以读取该队列并输出到控制台。
-
谢谢!但是,问题不在于使用 __ name __ == "__ main __": 行,因为该脚本从另一个脚本调用了系统调用,其中也有时间模块并且没有被保护。
-
@scotty3785 标准输出是从主进程复制的,但 jupyter 会重新定向(和后处理)标准输出并且不会告诉孩子们。这就是为什么 child
print在系统终端上可以正常工作而没有涉及重定向 tomfoolery 的原因。这在 pycharm 和其他一些人中也很常见。
标签: python-3.x multiprocessing python-multiprocessing