![](/img/trans.png)
[英]Run functions parallel in python 2.7 to use output of an function at the end of other functions
[英]How could a function look like, that can add an other function to a list of functions that run parallel in python?
def update():
while True:
loadData()
def main():
doStuff()
addToParallelFunctions(update)
doOtherStuff()
if __name__ == '__main__':
addToParallelFunctions(main)
addToParallelFunctions
function 怎么可能看起来像这样,以便 update 与main
并行运行,并且我可以添加其他函数以也与main
并行运行?
我试过了,但它暂停了主要的 function 并运行了另一个 function 直到她完成。
from multiprocessing import Process
def addProcess(*funcs):
for func in funcs:
Process(target=func).start()
我增强了您的示例:它对我有用。 主脚本和 function 继续。
import time
from multiprocessing import Process
def addProcess(*funcs):
for func in funcs:
Process(target=func).start()
def update():
for i in range(20):
print(i)
time.sleep(2)
def main():
print("start")
addProcess(update)
print("Main function continues")
if __name__ == '__main__':
addProcess(main)
print("main script continues")
下次,请发布一个可重现的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.