如何在python中使用多处理对吗? [英] how to use multiprocessing in python right?

查看:63
本文介绍了如何在python中使用多处理对吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import time
from multiprocessing import Process
start = time.perf_counter()


def sleep():
    print('Sleeping 1 second(s)...')
    time.sleep(1)
    return 'Done Sleeping...'

p1 = Process(target = sleep)
p2 = Process(target = sleep)
p1.start()
p2.start()
p1.join()
p2.join()
finish = time.perf_counter()
print(f'Finished in {round(finish-start, 2)} second(s)')

输出:

Finished in 0.17 second(s)

我尝试使用多处理,但是当我运行代码时,它在 0.17~ 秒内结束,而不是预期的 1,它根本没有启动该功能...

I tried to use multiprocessing, but when I run the code it`s over in 0.17~ seconds and not 1 as it supposed to be, it's not sets off the function at all...

如果我像这样放括号:

p1 = Process(target = sleep())
p2 = Process(target = sleep())

输出:

Sleeping 1 second(s)...
Sleeping 1 second(s)...
Finished in 2.35 second(s)

windows 10.python 3.7.4谢谢:)

windows 10. python 3.7.4 thank you:)

推荐答案

我已经解决了这个问题,为了让你的代码正常工作,你应该添加 if __name__ == '__main__'.您的两个新进程都需要访问您的 def sleep() 才能执行此操作,您必须通过 __name__ == "__main__" 将代码的可执行"部分分开,或者放置 def sleep() 在另一个文件中并从那里导出 from filename import sleep

I have solved the problem, in order to make your code work you should add if __name__ == '__main__'. Both of your new processes need to get access to your def sleep() in order to do it you must either separate "executable" part of your code by __name__ == "__main__" or put def sleep() in another file and export it from there from filename import sleep

import time
from multiprocessing import Process
start = time.perf_counter()


def sleep():
    print('Sleeping 1 second(s)...')
    time.sleep(1)
    return 'Done Sleeping...'


if __name__ == "__main__":
    p1 = Process(target = sleep)
    p2 = Process(target = sleep)
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    finish = time.perf_counter()
    print(f'Finished in {round(finish-start, 2)} second(s)')

希望答案对您有用.

Doug Hellmann 的网站表单手册Python 3 Standard Libaray by Example":

Site form book "The Python 3 Standard Libaray by Example" by Doug Hellmann:

线程和多处理示例之间的一个区别是额外的保护对于 __main__ 包含在多处理示例中.由于新流程的方式启动后,子进程需要能够导入包含目标函数的脚本.将应用程序的主要部分包装在对 __main__ 的检查中确保它确实导入模块时,不会在每个子节点中递归运行.另一种方法是导入来自单独脚本的目标函数.例如, multiprocessing_import_main.py使用在第二个模块中定义的工作函数.

One difference between the threading and multiprocessing examples is the extra protection for __main__ included in the multiprocessing examples. Due to the way the new processes are started, the child process needs to be able to import the script containing the target function. Wrapping the main part of the application in a check for __main__ ensures that it does not run recursively in each child as the module is imported. Another approach is to import the target function from a separate script. For example, multiprocessing_import_main.py uses a worker function defined in a second module.

这篇关于如何在python中使用多处理对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆