但是我的machine_thread没有运行,这是为什么
时间: 2023-08-16 11:06:05 浏览: 43
你的代码中有一个错误,即在创建 machine_thread 进程的时候没有指定目标函数,应该改为:
```python
machine_thread = Process(target=wg.run_machine_game)
```
这样就可以正常运行了。注意:这里指定的是函数对象,而不是函数的调用结果。
相关问题
我改了,可它依旧没有运行。我的run_machine_game代码明明只有print(1)
你可以检查一下是否有其他错误导致程序无法运行。你可以在代码中加入一些调试语句,如:
```python
if __name__ == '__main__':
print("main process start")
wg = WordGuessing()
print("WordGuessing instance created")
player_thread = Process(target=wg.run_player_game)
print("player_thread created")
machine_thread = Process(target=wg.run_machine_game)
print("machine_thread created")
player_thread.start()
print("player_thread started")
machine_thread.start()
print("machine_thread started")
player_thread.join()
print("player_thread finished")
machine_thread.join()
print("machine_thread finished")
```
这样可以输出一些信息,看看程序运行到哪里出了问题。另外,你可以尝试在 run_machine_game() 函数中加入一些其他的语句,比如:
```python
def run_machine_game():
print("machine game start")
print(1)
print("machine game end")
```
这样也可以帮助你确定问题所在。
player_thread.start() print("player_thread created") machine_thread.start() print("player_thread created") player_thread.join() print("player_thread created") machine_thread.join() print("player_thread created") 什么调试语句都失败了
你可以尝试在创建进程时将 target 参数指定为函数对象,而不是函数的调用结果。修改后的代码如下:
```python
if __name__ == '__main__':
wg = WordGuessing()
player_thread = Process(target=wg.run_player_game)
machine_thread = Process(target=wg.run_machine_game)
player_thread.start()
machine_thread.start()
player_thread.join()
machine_thread.join()
```
这样应该就可以正常运行了。请注意,函数对象不要加括号,即不要写成 `wg.run_player_game()`,而应该写成 `wg.run_player_game`。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)