简体   繁体   English

如何缓冲multiprocessing.Process内部和外部函数的所有print()?

[英]How to buffer all print() of the multiprocessing.Process inside and external functions?

I use multiprocessing for some tasks with multiple print() indicators inside them, so I can see how the process started/worked/ended in cmd. 我对其中带有多个print()指示器的某些任务使用了多重处理,因此我可以看到该过程如何在cmd中开始/工作/结束。 But, some tasks are quicker/slower, so print() order is chaotic. 但是,某些任务更快或更慢,因此print()顺序很混乱。 I need to find a way how to show all print() at once at the end of the process. 我需要找到一种方法,以便在过程结束时立即显示所有print()

Simplified example: 简化示例:

from multiprocessing import Process
from time import sleep


def runner(px_id):
    print('Start'+str(px_id))

    if px_id > 1:
        sleep(2)
    elif px_id == 1:
        sleep(5)

    print('Middle'+str(px_id))

    if px_id > 1:
        sleep(2)
    elif px_id == 1:
        sleep(5)

    print('End'+str(px_id))


if __name__ == '__main__':
    px_list = [1, 2, 3]

    for px in px_list:
        p = Process(target=runner, args=(px,))
        p.start()

If I run this code now, I'll receive: 如果我现在运行此代码,我将收到:

Start1
Start2
Start3
Middle2
Middle3
End2
End3
Middle1
End1

While I need something like: 虽然我需要这样的东西:

Start2
Middle2
End2
Start3
Middle3
End3
Start1
Middle1
End1

I still need processes to be async (so can't use join() ) but to print all print() at the end of a process. 我仍然需要使进程异步(因此不能使用join() ),但要在进程结束时打印所有print()。 Grateful for any advice :) 感谢任何建议:)

UPD: Using an array for buffering function messages is an OK solution, still need some help with buffering solutions from external functions. UPD:使用数组缓冲功能消息是一个不错的解决方案,但是在从外部函数缓冲解决方案方面仍需要一些帮助。 I mean, if I use some function inside other function I can't buffer all print() manually, so looking for some more complex solution :) 我的意思是,如果我在其他函数中使用某些函数,则无法手动缓冲所有print() ,因此需要寻找一些更复杂的解决方案:)

One option would be to have each instance buffer the output in an internal buffer. 一种选择是让每个实例在内部缓冲区中缓冲输出。 then output it at the end. 然后在最后输出。 You could have the main thread go and save each started process in an array. 您可以让主线程运行并将每个启动的进程保存在一个数组中。 then wait on all three to finish and finally go and grab the internal buffer from each process in the array and print them serially. 然后等待所有三个操作完成,最后从数组中的每个进程中获取内部缓冲区,并依次打印它们。

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

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