gpt4 book ai didi

queue - Python-多线程时间敏感任务

转载 作者:行者123 更新时间:2023-12-02 19:26:44 25 4
gpt4 key购买 nike

from random import randrange
from time import sleep
#import thread
from threading import Thread
from Queue import Queue
'''The idea is that there is a Seeker method that would search a location
for task, I have no idea how many task there will be, could be 1 could be 100.
Each task needs to be put into a thread, does its thing and finishes. I have
stripped down a lot of what this is really suppose to do just to focus on the
correct queuing and threading aspect of the program. The locking was just
me experimenting with locking'''
class Runner(Thread):
current_queue_size = 0
def __init__(self, queue):
self.queue = queue
data = queue.get()
self.ID = data[0]
self.timer = data[1]
#self.lock = data[2]
Runner.current_queue_size += 1
Thread.__init__(self)

def run(self):
#self.lock.acquire()
print "running {ID}, will run for: {t} seconds.".format(ID = self.ID,
t = self.timer)
print "Queue size: {s}".format(s = Runner.current_queue_size)
sleep(self.timer)
Runner.current_queue_size -= 1
print "{ID} done, terminating, ran for {t}".format(ID = self.ID,
t = self.timer)
print "Queue size: {s}".format(s = Runner.current_queue_size)
#self.lock.release()
sleep(1)
self.queue.task_done()

def seeker():
'''Gathers data that would need to enter its own thread.
For now it just uses a count and random numbers to assign
both a task ID and a time for each task'''
queue = Queue()
queue_item = {}
count = 1
#lock = thread.allocate_lock()
while (count <= 40):
random_number = randrange(1,350)
queue_item[count] = random_number
print "{count} dict ID {key}: value {val}".format(count = count, key = random_number,
val = random_number)
count += 1

for n in queue_item:
#queue.put((n,queue_item[n],lock))
queue.put((n,queue_item[n]))
'''I assume it is OK to put a tulip in and pull it out later'''
worker = Runner(queue)
worker.setDaemon(True)
worker.start()
worker.join()
'''Which one of these is necessary and why? The queue object
joining or the thread object'''

#queue.join()

if __name__ == '__main__':
seeker()

我已将大部分问题放在代码本身中,但要回顾一下要点(Python2.7):

  • 我想确保以后不会为自己造成大量内存泄漏。
  • 我注意到,当我在 putty 或 VNC 上以 40 的计数运行它时我的 linuxbox 并不总是能得到所有的输出,但是当我在 Windows 上使用 IDLE 和 Aptana。
  • 是的,我明白队列的目的是错开你的队列线程,这样您就不会淹没系统内存,而是处理任务手对时间敏感,因此需要尽快处理无论有多少,都会被检测到;我有发现当我有队列时我可以清楚地指示任务何时完成而不是让垃圾收集器猜测。
  • 我仍然不知道为什么我能够使用.join() 在线程或队列对象上。
  • 提示、技巧、一般帮助。
  • 感谢您的阅读。

最佳答案

如果我理解正确的话,您需要一个线程来监视某些内容以查看是否有需要完成的任务。如果找到一个任务,您希望该任务与搜索器和其他当前正在运行的任务并行运行。

如果是这种情况,那么我认为您的做法可能是错误的。看一下 GIL 在 Python 中的工作原理。我认为您可能真正想要的是多处理。

从 pydocs 中看一下:

CPython 实现细节:在 CPython 中,由于全局解释器锁,一次只有一个线程可以执行 Python 代码(尽管某些面向性能的库可能会克服这一限制)。如果您希望您的应用程序更好地利用多核机器的计算资源,建议您使用多处理。但是,如果您想同时运行多个 I/O 密集型任务,线程仍然是一个合适的模型。

关于queue - Python-多线程时间敏感任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11379187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com