代码之家  ›  专栏  ›  技术社区  ›  Mark Smith

Python 3.6-如何从守护进程线程中启动线程,并在其运行的函数完成后让子线程退出

  •  0
  • Mark Smith  · 技术社区  · 8 年前

    我正在学习Python(3.6)并以函数式风格编写一些代码(不使用类)——我有一些函数,它们是由manager()函数作为守护进程线程启动的。

    我需要在它自己的线程中启动一个额外的函数,该函数是从管理器函数作为守护进程线程启动的函数之一中传递的参数,因为如果它是从管理器函数中启动的,它会引发一个异常,因为它所期望的参数在启动时不存在。。。

    这是我的代码的一个表示形式和我遇到的问题-我希望能够在自己的线程中启动func4,并从func3中向其传递一个参数。。。func3将多次调用func4,因此我需要运行func4的线程在代码完成后立即终止。。。

    import threading
    
    threads = []
    
    
    def func1():
        // runs in its own thread and receives messages from a message queue
    
    def func2():
        // runs in its own thread and sends messages in a loop to a message queue
    
    def func3():
        // runs in its own thread and receives messages from a message queue, does some
        // processing, assigns values to a variable then passes variable to func4
    
    
    def func4(passed_variable_from func3):
        // performs actions on variable passed from func3 and I would like it to 
        // run in its own thread... IS THIS POSSIBLE?
    
    def manager():
    
    # Thread t1 
    t1 = threading.Thread(target=func1)
    t1.daemon = True
    threads.append(t1)
    
    # Thread t2
    t2 = threading.Thread(target=func2)
    t2.daemon = True
    threads.append(t2)
    t2.start()
    
    # Thread t3 
    t3 = threading.Thread(target=func3)
    t3.daemon = True
    threads.append(t3)
    t3.start()
    
    t1.start()
    for t in threads: 
        t.join()
    
    manager()
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   mhsmith    8 年前

    与所有其他线程函数一样,func4会在函数返回时导致其线程死亡,因此无需任何特殊操作。

    但是,如果func3将多次调用func4,那么为每次调用启动一个新线程可能会效率低下。尤其是如果您想以函数式风格编写,最好使用更高级别的API,如 ThreadPoolExecutor 发送您的电话。这还为您提供了一种使用 result add_done_callback 方法(视情况而定)。

        2
  •  1
  •   Mark Smith    8 年前

    在进行了大量的搜索和调查之后,我通过在上使用stackoverflow答案中的信息找到了问题的解决方案 threading

    基本上,我发现我可以使用以下代码从线程中启动一个新线程:

    t = threading.Thread(target=target_function,args=(args))
    t.daemon = True
    t.start()
    

    我已经知道如何使用此代码来启动线程,因为它或多或少与我在原始问题中列出的用于启动func1、func2和func3函数的代码相同,但我没有意识到的是,可以从在其自身线程中启动的函数中调用此代码。。。

    这是我的原始问题中的代码,上面直接插入了代码,以便func4在从func3启动后在自己的线程中运行,func3也在自己的线程中运行。。。

    import threading
    
    threads = []
    
    
    def func1():
        // runs in its own thread and receives messages from a message queue
    
    def func2():
        // runs in its own thread and sends messages in a loop to a message queue
    
    def func3():
        // runs in its own thread and receives messages from a message queue, does some
        // processing, assigns value to variable_from_func3
    
        t = threading.Thread(target=func4,args=(variable_from_func3))
        t.daemon = True
        t.start()
    
        // Code above starts func4 in its own thread passing variable_from_func3 as an arg
    
    
    def func4(variable_from func3):
        // performs actions on variable passed from func3  
        // This now runs in its own thread which dies once complete
    
    def manager():
    
    # Thread t1 
    t1 = threading.Thread(target=func1)
    t1.daemon = True
    threads.append(t1)
    
    # Thread t2
    t2 = threading.Thread(target=func2)
    t2.daemon = True
    threads.append(t2)
    t2.start()
    
    # Thread t3 
    t3 = threading.Thread(target=func3)
    t3.daemon = True
    threads.append(t3)
    t3.start()
    
    t1.start()
    for t in threads: 
    t.join()
    
    manager()
    

    我对此进行了测试,发现它可以根据需要工作,因为func1、2和3由manager函数启动,当manager函数终止时停止,func4在自己的线程中运行,当函数完成或manager函数终止时停止。

    我希望这对其他人有用。。。