我想两个线程同步:一个追加,另外空间PoPing从列表:如何同步python线程?
import threading
class Pop(threading.Thread):
def __init__(self, name, alist):
threading.Thread.__init__(self)
self.alist = alist
self.name = name
def run(self):
print "Starting " + self.name
self.pop_from_alist(self.alist)
print "Exiting " + self.name
def pop_from_alist(self, alist):
alist.pop(0)
def main():
alist = [1, 2]
# Create new thread
thread = Pop("Pop thread", alist)
for x in range(2):
alist.append(alist[-1]+1)
thread.start()
print "Exiting Main Thread"
print alist
main()
我怎样才能做到这一点,我应该用锁或使用的连接方法? 无法找到任何初学者的同步教程
请查看[Python队列](https://docs.python.org/2/library/queue.html)。让我知道你是否需要一个完整的答案! –