有没有办法调用 class 对象线程的方法并在该特定线程中拥有它 运行 ?
Is there a way to call a method of a class object thread and have it run in that specific thread?
上下文:
我有一个线程 class,它在 for 循环中被调用了 10 次。
对于 class 的每个对象,我为用户传入一个 username
,这由它在 for 循环中的位置决定,例如 user0, user1, user2
等。
我在 class 中创建了一个名为 new_message()
的方法,它只打印另一条消息。
在 class 的 def run()
方法中,我将 username
作为键和 new_message()
函数作为值添加到字典中
我的问题:
我试图为其中一位用户调用 new_message()
函数,希望该函数会在为该特定用户创建的线程中 运行,但我认为它 运行 在主线程中,导致我的下一行代码等待。
所以我的问题是:
有没有办法调用 class 对象线程的方法并将其 运行 放在该特定线程中?
请注意,这是我遇到的一个更大问题的代表,但我做了一个模仿我的代码的最小复制示例。
代码:
import time
import threading
dict = {
}
class go(threading.Thread):
def __init__(self, username):
threading.Thread.__init__(self)
self.username = username
def run(self):
dict[self.username] = self.new_message
for count in range(10):
print('Hello: '+self.username+'\n')
time.sleep(10)
def new_message(self):
for count in range(10):
print('How are you: '+self.username)
time.sleep(2)
for count in range(10):
username = ('user' + str(count))
go(username).start()
what_user = input('What user')
dict[what_user]()
Print('This line shouldn't be waiting to print')
Is there a way to call a method of a class object thread and have it run in that specific thread?
不,这是不可能的。您已经 start()
编辑了线程,这使它成为 运行 它的 run()
方法。你不能在那个特定的线程中调用这样的方法来 运行 它——它只会在你观察到的调用它的线程上 运行 (它在你的主线程中 运行 ) .
您需要其他线程(我们称它们为工作线程)与外部提交的任务合作才能实现这一点。例如,您可以让工作线程在完成初始工作后监听工作队列,以接收任务(即函数)和 运行 它们。
这是您的代码的快速实现:
import time
import threading
from queue import Queue
dict = {}
class Go(threading.Thread):
def __init__(self, username, work_queue):
threading.Thread.__init__(self)
self.username = username
self.work_queue = work_queue
def run(self):
self.initialize()
while True:
task = self.work_queue.get()
task()
self.work_queue.task_done()
def initialize(self):
dict[self.username] = {"func": self.new_message, "queue": self.work_queue}
for _ in range(10):
print("Hello: " + self.username + "\n")
time.sleep(10)
def new_message(self):
for _ in range(10):
print("How are you: " + self.username)
time.sleep(2)
for count in range(10):
username = "user" + str(count)
Go(username, Queue()).start()
what_user = input("What user")
selection = dict[what_user]
queue, method = selection["queue"], selection["func"]
queue.put(method)
print("This line shouldn't be waiting to print")
您会发现 "This line shouldn't be waiting to print"
现在确实不会等待。主线程将其作为任务放在所选工作线程的队列中。工作人员在完成 self.initialize()
调用后将其拾取。
上下文:
我有一个线程 class,它在 for 循环中被调用了 10 次。
对于 class 的每个对象,我为用户传入一个 username
,这由它在 for 循环中的位置决定,例如 user0, user1, user2
等。
我在 class 中创建了一个名为 new_message()
的方法,它只打印另一条消息。
在 class 的 def run()
方法中,我将 username
作为键和 new_message()
函数作为值添加到字典中
我的问题:
我试图为其中一位用户调用 new_message()
函数,希望该函数会在为该特定用户创建的线程中 运行,但我认为它 运行 在主线程中,导致我的下一行代码等待。
所以我的问题是:
有没有办法调用 class 对象线程的方法并将其 运行 放在该特定线程中?
请注意,这是我遇到的一个更大问题的代表,但我做了一个模仿我的代码的最小复制示例。
代码:
import time
import threading
dict = {
}
class go(threading.Thread):
def __init__(self, username):
threading.Thread.__init__(self)
self.username = username
def run(self):
dict[self.username] = self.new_message
for count in range(10):
print('Hello: '+self.username+'\n')
time.sleep(10)
def new_message(self):
for count in range(10):
print('How are you: '+self.username)
time.sleep(2)
for count in range(10):
username = ('user' + str(count))
go(username).start()
what_user = input('What user')
dict[what_user]()
Print('This line shouldn't be waiting to print')
Is there a way to call a method of a class object thread and have it run in that specific thread?
不,这是不可能的。您已经 start()
编辑了线程,这使它成为 运行 它的 run()
方法。你不能在那个特定的线程中调用这样的方法来 运行 它——它只会在你观察到的调用它的线程上 运行 (它在你的主线程中 运行 ) .
您需要其他线程(我们称它们为工作线程)与外部提交的任务合作才能实现这一点。例如,您可以让工作线程在完成初始工作后监听工作队列,以接收任务(即函数)和 运行 它们。
这是您的代码的快速实现:
import time
import threading
from queue import Queue
dict = {}
class Go(threading.Thread):
def __init__(self, username, work_queue):
threading.Thread.__init__(self)
self.username = username
self.work_queue = work_queue
def run(self):
self.initialize()
while True:
task = self.work_queue.get()
task()
self.work_queue.task_done()
def initialize(self):
dict[self.username] = {"func": self.new_message, "queue": self.work_queue}
for _ in range(10):
print("Hello: " + self.username + "\n")
time.sleep(10)
def new_message(self):
for _ in range(10):
print("How are you: " + self.username)
time.sleep(2)
for count in range(10):
username = "user" + str(count)
Go(username, Queue()).start()
what_user = input("What user")
selection = dict[what_user]
queue, method = selection["queue"], selection["func"]
queue.put(method)
print("This line shouldn't be waiting to print")
您会发现 "This line shouldn't be waiting to print"
现在确实不会等待。主线程将其作为任务放在所选工作线程的队列中。工作人员在完成 self.initialize()
调用后将其拾取。