简体   繁体   English

Python线程:等待线程停止然后执行函数

[英]Python threading: wait for thread to stop then execute function

I'm trying to run a function after my thread has completed but the function is not called.我正在尝试在线程完成后运行一个函数,但未调用该函数。 Code structure:代码结构:

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()

note: i removed 'self' for simplicity.注意:为简单起见,我删除了“自我”。

thread.join should tell me when the thread has finished but for some reason i still cant run the functiontocall. thread.join 应该告诉我线程何时完成,但由于某种原因我仍然无法运行 functiontocall。

Is this a bad way of organizing threads in general?一般来说,这是组织线程的一种不好的方式吗?

Edit: I can call the function after the thread is finished but I cannot access variables when the thread is running.编辑:我可以在线程完成后调用该函数,但在线程运行时我无法访问变量。 eg 0-100% progress for a progress bar in my GUI.例如,我的 GUI 中进度条的进度为 0-100%。 when I use:当我使用:

def watchthread():
    thread()
    thread.join()
    functiontocall()

I cannot update the status of the thread in my GUI.我无法在 GUI 中更新线程的状态。 It just waits until the calculations are finished then runs functiontocall().它只是等到计算完成然后运行 ​​functiontocall()。

Because you're using threads, once the thread had started Python will move onto the next thing, it will not wait for the thread to finish unless you've asked it to.因为您正在使用线程,一旦线程启动,Python 将转移到下一件事,除非您要求,否则它不会等待线程完成。

With your code, if you want to wait for the thread function to finish before moving on then it doesn't sound like you need threading, a normal function would run, complete, and then Python will move onto running functiontocall()使用您的代码,如果您想在继续之前等待线程函数完成,那么听起来您不需要线程,一个正常的函数将运行,完成,然后 Python 将继续运行 functiontocall()

If there's a reason you need to use threads which isn't coming across in the example then I would suggest using a thread.join()如果您需要使用示例中没有出现的线程,那么我建议您使用 thread.join()

threads = []  # list to hold threads if you have more than one
t = threading.Thread(target=run)
threads.append(t)

for thread in threads:  # wait for all threads to finish
    thread.join() 

functiontocall()  # will only run after all threads are done

Again, I'd suggest relooking at whether threads is what you need to use here as it doesn't seem apparent.同样,我建议重新考虑线程是否是您需要在此处使用的,因为它似乎并不明显。

To update this answer based on the new information, this may be the way you want to have a variable be accessible.要根据新信息更新此答案,这可能是您希望访问变量的方式。 In this case the threads would all update your class variable A, your GUI update function also reads this periodically and updates your GUI.在这种情况下,线程都会更新您的类变量 A,您的 GUI 更新函数也会定期读取它并更新您的 GUI。

class ThisClass():

    def __init__(self):
        self.A = 0

    def function_to_call(self):
        while self.A != 100:  # assuming this is a progress bar to 100%
            # update in GUI

    def run(self):
        # does calculations
        with lock: # to prevent issues with threads accessing variable at the same time
            self.A += calculations

    def progress(self):
        threads = []  # list to hold threads if you have more than one
        t = threading.Thread(target=run)
        threads.append(t)

        f = threading.Thread(target=self.function_to_call)
        threads.append(f)

        for thread in threads:
            thread.start()

        for thread in threads:  # wait for all threads to finish
            thread.join()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM