python - Python无法从线程更新
问题描述
我正在为另一个应用程序编写第三方插件。我需要在单独的线程中运行一些代码,但似乎没有在单独的线程中运行任何代码。
这是一个基本示例
from threading import Thread
from time import sleep
from _Framework.ControlSurface import ControlSurface
class Collab(ControlSurface):
def __init__(self, c_instance):
ControlSurface.__init__(self, c_instance)
# self.log_message is inherited from c_instance, logs
# to the main app's log folder
self.log_message("COLLAB INIT")
Thread(target=self.run_in_thread).start()
self.log_message("Thread should run")
sleep(5)
self.log_message("Still waiting")
sleep(5)
def run_in_thread(self):
sleep(1)
self.log_message("Called from thread")
输出将是
COLLAB INIT
Thread should run
Still Waiting
Called From thread
永远不会被记录。
我已经尝试了我能想到的所有变体,我已经尝试锁定线程。我尝试在线程中运行无限循环并验证线程是否仍在运行(它是),我尝试修改共享全局变量。线程中运行的任何内容似乎都无法输出任何内容,或影响任何其他变量。
解决方案
推荐阅读
- db2 - 将 XML PATH 示例代码从 SQL Server 转换为 DB2
- airflow-scheduler - 我可以使用默认值参数触发 dag 吗?
- android - 如何使三个图像可触摸
- python - 如何评估 HDBSCAN 文本簇?
- c# - 如何在 TelegramBotAPI 中创建具有多行的内联键盘?
- visual-studio-code - VSCode:如何找到我的工作区文件夹
- angular - 检查 API 服务是否已关闭或没有角度
- substrate - 为什么在调用智能合约函数时显示错误消息“system.ExtrinsicFailed”?
- php - 使用 PHP 防止 Amazon S3 上的热链接
- c# - DirectoryEntry 安全加密