ThreadPoolWorkers 如果他们创建一个线程就不会死
ThreadPoolWorkers that will not die if they create a thread
在我的 Slack RTM 消息处理函数中,我正在调用一组函数中的任何一个。独自一人,每个消息事件处理程序都会阻塞下一个,所以我开始在新线程中调用这些函数。这样做之后,我无法 exit()
我的程序,我为每个被调用的消息处理程序留下了 ThreadPoolExecutor-x_x
。
即使我将线程设置为 setDaemon=True
和 .join()
,ThreadPoolExecutors 仍然存在。
def exitFunc(sendfn, channel, thread, user, text, groups, groupdict, meta):
reply = 'bye'
sendfn(channel=channel, message = reply)
for thread in threading.enumerate():
print(thread.getName())
exit()
生成并挂起:
MainThread
ThreadPoolExecutor-0_0
ThreadPoolExecutor-0_1
ThreadPoolExecutor-3_0
ThreadPoolExecutor-3_1
ThreadPoolExecutor-3_2
exitFunc
ThreadPoolExecutor-3_3
当我不 运行 作为新线程的函数时,这些 ThreadPoolExecutors 似乎会闲逛,但它们确实让我的程序退出。
生成线程:
def __init__(self, token, username = None, icon_emoji = None, security = None):
...
slack.RTMClient.run_on(event='message')(self.readMessage)
def readMessage(self, **payload):
...
thread = Thread(target=fn['fn'], kwargs = fnargs)
thread.start()
作为sendfn传入的函数:
def sendMessage(self, channel, message, thread = None, username = None, icon_emoji = None):
if username == None:
username = self.default_username
if icon_emoji == None:
icon_emoji = self.default_icon_emoji
print('{} Send to {}: {}'.format(str(datetime.now()), channel, message))
self.webclient.chat_postMessage(
channel=channel,
text=message,
username=username,
icon_emoji=icon_emoji,
thread_ts=thread
)
我正在使用 slackclient 2.0.1 python 包。
这是一个XY problem.
不是我的线程没有死,而是我试图从线程中退出()。
Since exit() ultimately “only” raises an exception, it will only exit
the process when called from the main thread, and the exception is not
intercepted.
https://docs.python.org/2/library/sys.html#sys.exit
我将退出移至主线程,它退出正常。
在我的 Slack RTM 消息处理函数中,我正在调用一组函数中的任何一个。独自一人,每个消息事件处理程序都会阻塞下一个,所以我开始在新线程中调用这些函数。这样做之后,我无法 exit()
我的程序,我为每个被调用的消息处理程序留下了 ThreadPoolExecutor-x_x
。
即使我将线程设置为 setDaemon=True
和 .join()
,ThreadPoolExecutors 仍然存在。
def exitFunc(sendfn, channel, thread, user, text, groups, groupdict, meta):
reply = 'bye'
sendfn(channel=channel, message = reply)
for thread in threading.enumerate():
print(thread.getName())
exit()
生成并挂起:
MainThread
ThreadPoolExecutor-0_0
ThreadPoolExecutor-0_1
ThreadPoolExecutor-3_0
ThreadPoolExecutor-3_1
ThreadPoolExecutor-3_2
exitFunc
ThreadPoolExecutor-3_3
当我不 运行 作为新线程的函数时,这些 ThreadPoolExecutors 似乎会闲逛,但它们确实让我的程序退出。
生成线程:
def __init__(self, token, username = None, icon_emoji = None, security = None):
...
slack.RTMClient.run_on(event='message')(self.readMessage)
def readMessage(self, **payload):
...
thread = Thread(target=fn['fn'], kwargs = fnargs)
thread.start()
作为sendfn传入的函数:
def sendMessage(self, channel, message, thread = None, username = None, icon_emoji = None):
if username == None:
username = self.default_username
if icon_emoji == None:
icon_emoji = self.default_icon_emoji
print('{} Send to {}: {}'.format(str(datetime.now()), channel, message))
self.webclient.chat_postMessage(
channel=channel,
text=message,
username=username,
icon_emoji=icon_emoji,
thread_ts=thread
)
我正在使用 slackclient 2.0.1 python 包。
这是一个XY problem.
不是我的线程没有死,而是我试图从线程中退出()。
Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.
https://docs.python.org/2/library/sys.html#sys.exit
我将退出移至主线程,它退出正常。