我有这样的代码:
from multiprocessing import Process, cpu_count
def readplayerinfo():
y=0
Gameon = True
while Gameon:
y+=1
print('y',y)
if y == 50:
Gameon = False
return None
def main():
islooping = True
x=0
a = Process(target=readplayerinfo,args = ())
a.start()
while islooping:
print('x',x)
x+=1
if x == 100:
islooping = False
a.join()
if __name__ == '__main__':
main()该程序的目标是创建两个进程,在每个进程中执行一个while循环,同时打印y和x(速度最快的进程显然先打印出来)。
但是当我运行它的时候,终端只显示“x0”,它就会结冰。
我尽了最大努力去研究,但这是我第一次尝试多重处理。
所以我的问题是,这是如何使这个多处理工作的?
编辑:我被告知,空闲的ide有变异处理的问题,所以我转而使用ubuntu20.04中的终端,然后我的输出只有y被打印,然后它冻结了,no也被打印出来了。另外,我还打印了(cpu_count()),它还给了我4,所以我不认为这是硬件问题
发布于 2022-04-29 00:50:07
我尝试了很多东西,但唯一的方法是让x和y交替打印,是做两个不同的过程。X和y将一个接一个地打印,但是由于启动processe.Plus的开销,它们之间会有轻微的延迟,我不知道为什么,但是空闲在多处理中不能很好地工作。因此,您需要找到另一个IDE,或者在终端中使用“python3(Nameofthefile)..py”。
from multiprocessing import Process, cpu_count
def secondprocess():
y=0
Gameon = True
while Gameon:
y+=1
print('y',y)
if y == 50000:
Gameon = False
return None
def firstprocess():
x = 0
islooping = True
while islooping:
x+=1
print('x',x)
if x == 50000:
islooping = False
def main():
a = Process(target=secondprocess,args = ())
b = Process(target=firstprocess,args = ())
a.start()
b.start()
a.join()
b.join()
if __name__ == '__main__':
main()https://stackoverflow.com/questions/71905775
复制相似问题