Python 具有复杂功能的多处理
Python multiprocessing with complex functions
我知道这个问题已经被问过了,但我已经做了广泛的研究,无法解决我的问题。
我的问题与运行并行复杂函数有关。
我正在使用 selenium webdriver 自动从一个网站下载文件并将其上传到另一个网站,我希望这些功能同时 运行。它们都使用 while 循环 并具有分层条件语句。我无法同时获得 运行 的功能,希望得到帮助。我的代码如下:
import multiprocessing
def auto_download():
# function logic here
def auto_upload():
# function logic here
if __name__ == '__main__':
p1 = multiprocessing.Process(name='auto download', target=auto_download())
p2 = multiprocessing.Process(name='auto upload', target=auto_upload())
p1.start()
p2.start()
此代码 运行 是第一个函数 auto_download() 但从未启动第二个函数。
但是,如果我 运行 来自 的以下代码,这是相同的想法但具有更简单的功能,它工作正常。
import multiprocessing
import time
def add():
while True:
print (1)
time.sleep(3)
def sud():
while True:
print(0)
time.sleep(3)
if __name__ == '__main__':
p1 = multiprocessing.Process(name='p1', target=add)
p = multiprocessing.Process(name='p', target=sud)
p1.start()
p.start()
我的问题是否源于我同时尝试 运行 的函数的复杂性?提前感谢您的帮助!
编辑:解决方案(感谢 Raw Dawg) is that I was calling the function directly instead of in the process object. This is different than the solutions for 。以下代码修复了问题:
p1 = multiprocessing.Process(name='auto download', target=auto_download)
p2 = multiprocessing.Process(name='auto upload', target=auto_upload)
p1.start()
p2.start()
我想我在这里发现了问题 - 当您使用
定义流程时
p1 = multiprocessing.Process(name='auto download', target=auto_download())
你是运行函数auto_download()
。您实际上是在主流程中启动此功能,而不是像您希望的那样在 Process 对象中启动。由于这个原因 p1.start()
没有你想要的效果,因为你没有同时 运行 这些功能。请注意,在 Process 签名的第二个示例中,您将其定义为
p1 = multiprocessing.Process(name='p1', target=add)
add
后没有括号。使用以下代码重试:
p1 = multiprocessing.Process(name='auto download', target=auto_download)
p2 = multiprocessing.Process(name='auto upload', target=auto_upload)
p1.start()
p2.start()
我知道这个问题已经被问过了,但我已经做了广泛的研究,无法解决我的问题。
我的问题与运行并行复杂函数有关。
我正在使用 selenium webdriver 自动从一个网站下载文件并将其上传到另一个网站,我希望这些功能同时 运行。它们都使用 while 循环 并具有分层条件语句。我无法同时获得 运行 的功能,希望得到帮助。我的代码如下:
import multiprocessing
def auto_download():
# function logic here
def auto_upload():
# function logic here
if __name__ == '__main__':
p1 = multiprocessing.Process(name='auto download', target=auto_download())
p2 = multiprocessing.Process(name='auto upload', target=auto_upload())
p1.start()
p2.start()
此代码 运行 是第一个函数 auto_download() 但从未启动第二个函数。
但是,如果我 运行 来自
import multiprocessing
import time
def add():
while True:
print (1)
time.sleep(3)
def sud():
while True:
print(0)
time.sleep(3)
if __name__ == '__main__':
p1 = multiprocessing.Process(name='p1', target=add)
p = multiprocessing.Process(name='p', target=sud)
p1.start()
p.start()
我的问题是否源于我同时尝试 运行 的函数的复杂性?提前感谢您的帮助!
编辑:解决方案(感谢 Raw Dawg) is that I was calling the function directly instead of in the process object. This is different than the solutions for
p1 = multiprocessing.Process(name='auto download', target=auto_download)
p2 = multiprocessing.Process(name='auto upload', target=auto_upload)
p1.start()
p2.start()
我想我在这里发现了问题 - 当您使用
定义流程时p1 = multiprocessing.Process(name='auto download', target=auto_download())
你是运行函数auto_download()
。您实际上是在主流程中启动此功能,而不是像您希望的那样在 Process 对象中启动。由于这个原因 p1.start()
没有你想要的效果,因为你没有同时 运行 这些功能。请注意,在 Process 签名的第二个示例中,您将其定义为
p1 = multiprocessing.Process(name='p1', target=add)
add
后没有括号。使用以下代码重试:
p1 = multiprocessing.Process(name='auto download', target=auto_download)
p2 = multiprocessing.Process(name='auto upload', target=auto_upload)
p1.start()
p2.start()