简体   繁体   English

[多处理python]:没有output

[英][multiprocessing python]: no output

can someone explain me please why when i tried to execute this below exemple, i have no result.有人可以解释一下为什么当我尝试执行以下示例时,我没有结果。 Also i tried to redirect the output in a file but in vain.我还尝试将 output 重定向到文件中,但徒劳无功。

from multiprocessing import Process
def proc(i):
    print(f'I am Process {i}')
if __name__ ==  '__main__':
    for i in range(10):
        Process(target=proc, args=(i,)).start()

Normally, i have as output:通常,我有 output:

I am Process 6
I am Process 2
I am Process 0
I am Process 3
I am Process 7
I am Process 4
I am Process 8
I am Process 1
I am Process 5
I am Process 9

But in my case, i have no result.但就我而言,我没有结果。

It might be a typo in if _name_ == '__main__': missing an underscore. if _name_ == '__main__':缺少下划线可能是错字。

Running:跑步:

from multiprocessing import Process

def proc(i):
    print(f'I am Process {i}')

if __name__ == '__main__':
    for i in range(10):
        Process(target=proc, args=(i,)).start()

on my machine returns the correct output在我的机器上返回正确的 output

You need to force flush otherwise the output will only be visible if the buffer becomes full:您需要强制刷新,否则 output 只有在缓冲区已满时才可见:

def proc(i):
    print(f'I am Process {i}')
    sys.stdout.flush()

PS: You also have a typo when checking for for __name__ . PS:您在检查__name__时也有错字。 It should be if __name__ == '__main__': if __name__ == '__main__':

If you have to run under Notebook (and have moved your worker function to an external module as required) and you want to see the printout within Notebook, you need to arrange for the printing to be done by the main (launching) process.如果您必须在 Notebook 下运行(并且已根据需要将您的工作人员 function 移动到外部模块)并且您希望在 Notebook 中查看打印输出,则需要安排打印由主(启动)过程完成。 That means the launched processes need to return the strings to be printed back to the launching process.这意味着启动的进程需要将要打印的字符串返回给启动进程。 There are several ways to do that.有几种方法可以做到这一点。 Here I am using a multiprocessing pool with a callback function so that as soon as a result is ready, the main process is called back with the result:在这里,我使用带有回调 function 的多处理池,以便一旦结果准备好,主进程就会被回调并返回结果:

from multiprocessing import Pool
from workers import proc

def on_completion(result):
    print(result)

if __name__ ==  '__main__':
    pool = Pool(10)
    results = [pool.apply_async(proc, args=(i,), callback=on_completion) for i in range(10)]
    pool.close()
    pool.join() # wait for worker processes to exit

A second and simpler way:第二种更简单的方法:

from multiprocessing import Pool
from workers import proc


if __name__ ==  '__main__':
    pool = Pool(10)
    results = pool.imap_unordered(proc, range(10))
    # print the results as they become available:
    for result in results:
        print(result)

Using ProcessPoolExecutor:使用 ProcessPoolExecutor:

from concurrent.futures import ProcessPoolExecutor, as_completed
from workers import proc


if __name__ ==  '__main__':
    with ProcessPoolExecutor(max_workers=10) as executor:
        futures = [executor.submit(proc, i) for i in range(10)]
        for f in as_completed(futures):
            print(f.result())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM