Python和多处理

问题描述 投票:0回答:1
import multiprocessing

def main(a):
    while True:
        print(a)

p = multiprocessing.process(target=main(1))
p0 = multiprocessing.process(target=main(2))
p.start()
p0.start()

如果运行代码,它只会打印:

1
1
1
1
1
1
1
1

actullay应该输出这样的内容

1
2
1
1
2
2
2
1
2
1
2

我该如何解决:自从今天早上以来,我一直在进行研究。请发送帮助。

python python-3.x parallel-processing multiprocessing
1个回答
0
投票

这符合您的要求吗?

from multiprocessing import Process

def fun(a):
    while True:
        print(a)

if __name__ == '__main__':
    p = Process(target=fun, args=(1,)) # that's how you should pass arguments
    p.start()
    p1 = Process(target=fun, args=(2,))  
    p1.start()

输出:

1
2
1
2
2
2
2
2
2
1
© www.soinside.com 2019 - 2024. All rights reserved.