python-3.x - 来自 if 语句的多处理
问题描述
我有一个关于线程和/或多处理的快速问题。是否有任何简单的杀死线程的方法,是否可以从 if 语句启动进程。
我对编码很陌生,不知道编码中的大术语,所以我很难理解其他一些答案。
from multiprocessing import Process
done = False
def a():
print('a')
p = Process(target = a)
while not done:
userInput = input('')
if userInput == '1':
p.start()
if userInput == '2':
done = True
import threading
done = False
def a():
print('a')
p = threading.Thread(target = a)
while not done:
userInput = input('')
if userInput == '1':
p.start()
if userInput == '2':
done = True
我希望当我输入 1 时它会输出 a 但它什么也没做。当我再次尝试时,它给了我错误,AssertionError: cannot start a process两次。我能做些什么来解决这个问题。我知道线程工作一次,但如果我想第二次运行,我必须添加什么。
解决方案
推荐阅读
- wordpress - 无法在 elementor 中加载预览问题/错误
- python - 如何在我的个人字段中自动为用户设置外键集
- python - Python tkinter 画布没有做任何事情
- angular - Angular Button Disabled/Enable back 不适用于 Formly(反应形式)
- sql - 将时间戳转置到开始和结束时间戳列
- powerbi - 如何将 Power BI 中的新字段添加到使用 PowerBIIntegration 集成的现有 PowerApp?
- python - 如何在火花中将一个拆分为多个?
- firebase - firebase 实时数据库查询以查找数据
- java - Broadleaf Commerce Embedded Solr 无法使用 root 用户运行
- c++ - !root 和 root!=NULL 有什么区别