Python多进程函数无返回值问题:游戏逻辑无法终止主进程
问题:Python多进程游戏中进程同步与终止问题
我是一名中学生,正在开发一款仅用Python打印输出的游戏,需要用到multiprocessing多进程功能,但游戏函数里有input()输入操作,不能放进多进程否则会报错。现在的问题是,我期望timer或play函数任意一个结束后就能终止主进程,让两者同时运行,但实际函数返回后程序还会继续执行,最终打印出None。
原代码
import multiprocessing import time import random g = "Something from the list: " def timer(seconds): print(f"Sleeping for {seconds} second(s)") time.sleep(seconds) print( ) return "Done sleeping" def anything(y): print(y) def play(thing,harga): while True: t = thing price = harga[f"{thing}"] y = "-" h_1 = 54 h = random.randint(0,h_1) if h == 0: print(f"{t:{y}<{h_1+len(t)}}") ans = input("Do something!: ") if ans == thing: print(f"Great the price is {price} Credits!") return "True" else: h1 = (h_1 - h) print(f"{y:{y}<{h}}{t:{y}<{h1 + len(t)}}") ans = input("Do something!: ") if ans == thing: print(f"Great the price is {price} Credits!") return "True" def idk( ): time.sleep(0.1) list = ["Cheese","Pickle"] y = random.choice(list) anything(y) prilist = {"Cheese": 100,"Pickle":50} play(y,prilist) p1 = multiprocessing.Process(target=timer,args=[3]) def play1( ): v = p1.start( ) if v == "Done sleeping": return "Gagal" v2 = idk( ) if v2 == "True": return "True" p1.join( ) print(play1( ))
原程序输出
Sleeping for 3 second(s) Cheese ---------------Cheese---------------------------------------Do something!: Cheese Great the price is 100 Credits! None [Program finished]
解决方案
问题分析
p1.start()不会返回进程的返回值,v == "Done sleeping"永远不成立idk()函数未返回play()的执行结果,导致v2始终为None,最终play1()无返回值,打印None- 未监听两个任务的完成状态,无法实现任意任务结束后终止主进程
修改后的代码
import multiprocessing import time import random g = "Something from the list: " def timer(seconds, done_event): print(f"Sleeping for {seconds} second(s)") time.sleep(seconds) print() # 触发事件通知主进程:timer已完成 done_event.set() return "Done sleeping" def anything(y): print(y) def play(thing, harga, done_event): while True: t = thing price = harga[f"{thing}"] y = "-" h_1 = 54 h = random.randint(0, h_1) if h == 0: print(f"{t:{y}<{h_1+len(t)}}") else: h1 = (h_1 - h) print(f"{y:{y}<{h}}{t:{y}<{h1 + len(t)}}") ans = input("Do something!: ") if ans == thing: print(f"Great the price is {price} Credits!") # 触发事件通知主进程:play已完成 done_event.set() return "True" def idk(done_event): time.sleep(0.1) item_list = ["Cheese","Pickle"] y = random.choice(item_list) anything(y) prilist = {"Cheese": 100,"Pickle":50} # 返回play函数的执行结果 return play(y, prilist, done_event) def play1(): # 创建进程间通信的事件对象 done_event = multiprocessing.Event() # 启动timer进程,传入事件对象 p1 = multiprocessing.Process(target=timer, args=[3, done_event]) p1.start() # 在主进程执行play任务(避免input放入多进程报错) play_result = idk(done_event) # 等待任意一个任务触发完成事件 done_event.wait() # 终止未完成的进程 if p1.is_alive(): p1.terminate() p1.join() # 返回对应结果 return "True" if play_result == "True" else "Gagal" print(play1())
修改说明
- 用
multiprocessing.Event()实现进程间通信:timer和play完成时都会触发事件,主进程通过wait()监听,任意任务完成就会终止等待 - 修复
idk()的返回值问题,让它传递play()的结果,确保play1()能拿到有效返回值 - 事件触发后,主动终止未完成的进程,避免无效执行
- 保留
play()在主进程执行,规避input()放入多进程的报错问题
内容的提问来源于stack exchange,提问作者user24159246
相关产品推荐
相关产品推荐

