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

Python 3.6-如何从守护进程线程中启动线程,并在其运行的函数完成后让子线程退出,python,multithreading,python-3.x,Python,Multithreading,Python 3.x,我正在学习Python(3.6)并以函数式(不使用类)编写一些代码——我有一些函数,它们是由manager()函数作为守护进程线程启动的 我需要在它自己的线程中启动一个额外的函数,这是从管理器函数作为守护进程线程启动的函数之一中传递的参数,因为如果它是从管理器函数中启动的,它会抛出一个异常,因为它所期望的参数在启动时不存在 这是我的代码的一个表示形式和我遇到的问题-我希望能够在自己的线程中启动func4,并从func3中向它传递一个参数。。。func3将多次调用func4,因此我需要运行func

我正在学习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()

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


但是,如果func3将多次调用func4,那么为每次调用启动一个新线程可能效率低下。特别是如果您希望以函数式风格编写,那么最好使用更高级别的API,例如分派调用。这也为您提供了一种干净的方法来检索每个调用的返回值,可以根据需要使用or方法。

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


但是,如果func3将多次调用func4,那么为每次调用启动一个新线程可能效率低下。特别是如果您希望以函数式风格编写,那么最好使用更高级别的API,例如分派调用。这也为您提供了一种干净的方法来检索每个调用的返回值,根据需要使用or方法。

经过大量搜索和调查,我能够通过使用上的此stackoverflow答案中的信息找到问题的解决方案

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

t = threading.Thread(target=target_function,args=(args))
t.daemon = True
t.start()
我已经知道如何使用此代码来启动线程,因为它或多或少与我在原始问题中列出的用于启动func1、func2和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()
这是我的原始问题中的代码,上面直接插入了代码,这样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由管理器函数启动,当管理器函数终止时停止,func4在自己的线程中运行,当函数完成或管理器函数终止时停止


我希望这将对其他人有用…

经过大量的搜索和调查,我能够通过使用上的答案中的信息找到问题的解决方案

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

t = threading.Thread(target=target_function,args=(args))
t.daemon = True
t.start()
我已经知道如何使用此代码来启动线程,因为它或多或少与我在原始问题中列出的用于启动func1、func2和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()
这是我的原始问题中的代码,上面直接插入了代码,这样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由管理器函数启动,当管理器函数终止时停止,func4在自己的线程中运行,当函数完成或管理器函数终止时停止


我希望这对其他人有用…

谢谢,我将查看您的建议并返回给您-同时,func3处理队列中的消息,并为每条消息向func4传递一个参数(每秒最多20个…)我不确定func4是否在它自己的线程中运行,因为我没有这样启动它,也不能将它作为守护进程线程运行,因为如果我尝试从管理器启动func4,它接受参数会导致异常。。。(请原谅我的新手性)谢谢,我会检查你的建议,然后回来找你-同时,func3处理队列中的消息,并为每条消息(每秒最多20条)向func4传递一个参数我不确定func4是否在它自己的线程中运行,因为我没有这样启动它,也不能将它作为守护进程线程运行,因为如果我尝试从管理器启动func4,它接受参数会导致异常。。。(请原谅我的新手脾气)