[英]Unable to share values between processes in Multiprocessing python
I tried to run two scripts using multiprocessing as follows:我尝试使用多处理运行两个脚本,如下所示:
import multiprocessing
files=["file1","file2"]
if __name__ == "__main__":
for name in files:
process = multiprocessing.Process(target=lambda: __import__(name))
process.start()
I'm not able to pass the arguments while initiating the process because I get the error as lambda function accepts 0 arguments, but 2 were given .我无法在启动过程时传递参数,因为我收到错误,因为lambda 函数接受 0 个参数,但给出了 2 个。
I need to share a variable's data between the scripts.我需要在脚本之间共享变量的数据。 Any suggestions on how to resolve this.
有关如何解决此问题的任何建议。
Note: The functions in scripts were not able to run when the functions are imported and executed in multiprocessing.
注意:在多处理中导入和执行函数时,脚本中的函数无法运行。 Hence I choose this approach.
因此我选择了这种方法。
you need to pass the function name as a target, not the file.您需要将函数名称作为目标传递,而不是文件。
# Importing functions
from service.file import get_service
from service.file2 import get_another
from multiprocessing import process
def run_in_parallel(functions:list):
proc = []
for fn in fns:
p = Process(target=fn, args=(data))
p.start()
proc.append(p)
if __name__=='__main__':
func = [get_service, get_another]
run_in_parallel(func)
create a list of a process named proc
here, starting process one by one and inserting it into proc.在这里创建一个名为
proc
的进程列表,一个一个的启动进程并插入到proc中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.