【发布时间】:2020-03-04 19:36:58
【问题描述】:
我试图在我的线程完成后运行一个函数,但该函数没有被调用。代码结构:
class():
def functiontocall() # uses data calculated in thread for plotting. only works when thread is complete
do something with self.A
def watchthread():
thread()
functiontocall()
# since this function depends on variable A, it throws an error.
# I tried: if thread.join == True: functiontocall but this did not call the function.
def thread():
def run():
pythoncom.CoInitialize()
--- do stuff --
for i in 1000:
thousands of calculations while updating state in GUI ---
A = result
self.A = A
thread = threading.Thread(target=run)
thread.start()
注意:为简单起见,我删除了“self”。
thread.join 应该告诉我线程何时完成但由于某种原因我仍然无法运行函数调用。
这是一种组织线程的坏方法吗?
编辑:我可以在线程完成后调用该函数,但在线程运行时我无法访问变量。例如我的 GUI 中进度条的进度为 0-100%。当我使用时:
def watchthread():
thread()
thread.join()
functiontocall()
我无法在我的 GUI 中更新线程的状态。它只是等到计算完成然后运行 functiontocall()。
【问题讨论】:
标签: python python-multithreading