Python 3.3-将变量发送到线程
我不确定这是否是最佳实践或其他什么,但我有一个线程运行,每5秒激活一次,检查列表中的参数,在该线程之外,我有一个while循环运行,它将使用while循环之外的函数将参数插入列表中(这部分工作正常),但是,该列表在线程内部不会更新,如何将其发送到线程中 即Python 3.3-将变量发送到线程,python,multithreading,python-3.x,python-3.3,Python,Multithreading,Python 3.x,Python 3.3,我不确定这是否是最佳实践或其他什么,但我有一个线程运行,每5秒激活一次,检查列表中的参数,在该线程之外,我有一个while循环运行,它将使用while循环之外的函数将参数插入列表中(这部分工作正常),但是,该列表在线程内部不会更新,如何将其发送到线程中 即 我的意思是,它比这要复杂得多,而且粘贴到这里的时间太长,但我刚才添加的部分不起作用,我做错了什么?您的线程函数立即退出。。。它执行一次时间检查,根据上次时间的初始值打印mylist的第一个元素并退出 除了示例代码没有显示变量初始化的方式/位置
我的意思是,它比这要复杂得多,而且粘贴到这里的时间太长,但我刚才添加的部分不起作用,我做错了什么?您的线程函数立即退出。。。它执行一次时间检查,根据上次时间的初始值打印mylist的第一个元素并退出 除了示例代码没有显示变量初始化的方式/位置之外,线程函数需要如下所示:
def myfunction():
while True: # loop forever
if time.time() > 5 + last_time:
last_time = time.time()
print(mylist[0]) # doesn't print anything
time.sleep(1) # wait for one second for mylist to be updated, yield to main thread
注意:如果不在线程中添加sleep
,它将在繁忙循环中等待,而不会将控制返回主线程(并消耗大量CPU)
另外,请注意线程将始终打印相同的值(mylist的第一个元素)。由于addtolist
附加到列表的末尾,因此不会显示新值。您可能需要将print(mylist[0])
更改为print(mylist[-1])
以显示最后插入的元素
最后,有更好的方法来实现这一点。见第17.1.5节。条件对象。线程函数立即退出。。。它执行一次时间检查,根据上次时间的初始值打印mylist的第一个元素并退出 除了示例代码没有显示变量初始化的方式/位置之外,线程函数需要如下所示:
def myfunction():
while True: # loop forever
if time.time() > 5 + last_time:
last_time = time.time()
print(mylist[0]) # doesn't print anything
time.sleep(1) # wait for one second for mylist to be updated, yield to main thread
注意:如果不在线程中添加sleep
,它将在繁忙循环中等待,而不会将控制返回主线程(并消耗大量CPU)
另外,请注意线程将始终打印相同的值(mylist的第一个元素)。由于addtolist
附加到列表的末尾,因此不会显示新值。您可能需要将print(mylist[0])
更改为print(mylist[-1])
以显示最后插入的元素
最后,有更好的方法来实现这一点。见第17.1.5节。条件对象。线程函数立即退出。。。它执行一次时间检查,根据上次时间的初始值打印mylist的第一个元素并退出 除了示例代码没有显示变量初始化的方式/位置之外,线程函数需要如下所示:
def myfunction():
while True: # loop forever
if time.time() > 5 + last_time:
last_time = time.time()
print(mylist[0]) # doesn't print anything
time.sleep(1) # wait for one second for mylist to be updated, yield to main thread
注意:如果不在线程中添加sleep
,它将在繁忙循环中等待,而不会将控制返回主线程(并消耗大量CPU)
另外,请注意线程将始终打印相同的值(mylist的第一个元素)。由于addtolist
附加到列表的末尾,因此不会显示新值。您可能需要将print(mylist[0])
更改为print(mylist[-1])
以显示最后插入的元素
最后,有更好的方法来实现这一点。见第17.1.5节。条件对象。线程函数立即退出。。。它执行一次时间检查,根据上次时间的初始值打印mylist的第一个元素并退出 除了示例代码没有显示变量初始化的方式/位置之外,线程函数需要如下所示:
def myfunction():
while True: # loop forever
if time.time() > 5 + last_time:
last_time = time.time()
print(mylist[0]) # doesn't print anything
time.sleep(1) # wait for one second for mylist to be updated, yield to main thread
注意:如果不在线程中添加sleep
,它将在繁忙循环中等待,而不会将控制返回主线程(并消耗大量CPU)
另外,请注意线程将始终打印相同的值(mylist的第一个元素)。由于addtolist
附加到列表的末尾,因此不会显示新值。您可能需要将print(mylist[0])
更改为print(mylist[-1])
以显示最后插入的元素
最后,有更好的方法来实现这一点。见第17.1.5节。条件对象。我推荐另一种方法来实现具有某种状态的线程。不要使用全局变量,而是使用
threading.Thread
继承
例如,您的问题可以通过以下方式解决:
import threading
import time
class MyThread(threading.Thread):
def __init__(self, some_list=None):
super(MyThread, self).__init__()
self.list = some_list if some_list is not None else []
self.event = threading.Event() # Used for stop the thread
def run(self):
while not self.event.isSet():
# Do your thread thing here.
time.sleep(1)
print(self.list)
def add_elem(self, elem):
self.list.append(elem) # Extend or append, is up to you.
def stop(self):
self.event.set()
if __name__ == '__main__':
thread = MyThread() # Thread instance.
thread.start() # Start the thread.
thread.add_elem(10) # Add some element.
time.sleep(3) # Wait 3 secs.
thread.add_elem(100) # Add another element.
time.sleep(3) # Wait 3 secs.
thread.stop() # Stop thread.
通过这种方式,您甚至可以向线程添加更多的“行为”,您可以添加诸如remove
、sort
等函数
另一方面,如果你正在执行的任务被执行了一段时间(在你的情况下是5秒),你应该考虑使用
< P>我建议你另一个实现有状态的线程的方法。不要使用全局变量,而是使用threading.Thread
继承
例如,您的问题可以通过以下方式解决:
import threading
import time
class MyThread(threading.Thread):
def __init__(self, some_list=None):
super(MyThread, self).__init__()
self.list = some_list if some_list is not None else []
self.event = threading.Event() # Used for stop the thread
def run(self):
while not self.event.isSet():
# Do your thread thing here.
time.sleep(1)
print(self.list)
def add_elem(self, elem):
self.list.append(elem) # Extend or append, is up to you.
def stop(self):
self.event.set()
if __name__ == '__main__':
thread = MyThread() # Thread instance.
thread.start() # Start the thread.
thread.add_elem(10) # Add some element.
time.sleep(3) # Wait 3 secs.
thread.add_elem(100) # Add another element.
time.sleep(3) # Wait 3 secs.
thread.stop() # Stop thread.
通过这种方式,您甚至可以向线程添加更多的“行为”,您可以添加诸如remove
、sort
等函数
另一方面,如果你正在执行的任务被执行了一段时间(在你的情况下是5秒),你应该考虑使用
< P>我建议你另一个实现有状态的线程的方法。不要使用全局变量,而是使用threading.Thread
继承
例如,您的问题可以通过以下方式解决:
import threading
import time
class MyThread(threading.Thread):
def __init__(self, some_list=None):
super(MyThread, self).__init__()
self.list = some_list if some_list is not None else []
self.event = threading.Event() # Used for stop the thread
def run(self):
while not self.event.isSet():
# Do your thread thing here.
time.sleep(1)
print(self.list)
def add_elem(self, elem):
self.list.append(elem) # Extend or append, is up to you.
def stop(self):
self.event.set()
if __name__ == '__main__':
thread = MyThread() # Thread instance.
thread.start() # Start the thread.
thread.add_elem(10) # Add some element.
time.sleep(3) # Wait 3 secs.
thread.add_elem(100) # Add another element.
time.sleep(3) # Wait 3 secs.
thread.stop() # Stop thread.
通过这种方式,您甚至可以向线程添加更多的“行为”,您可以添加诸如remove
、sort
等函数
另一方面,如果您正在执行的任务超过了一段时间(在您的情况下是5秒),您应该