python 中类似生产者/消费者的多线程

发布于 2025-01-04 00:30:24 字数 675 浏览 2 评论 0原文

一个典型的生产者-消费者问题在Python中解决如下:

from queue import Queue
job_queue = Queue(maxsize=10)    

def manager():
    while i_have_some_job_do:
        job = get_data_from_somewhere()
        job_queue.put(job) #blocks only if queue is currently full

def worker():
    while True:
        data = job_queue.get() # blocks until data available
        #get things done

但是我有生产者/消费者问题的一种变体(严格来说不是一个,所以让我称之为经理-工人):

经理将一些工作放入队列中,然后工人应该继续获得工作并完成工作。但是,当工作线程获取作业时,它不会从队列中删除该作业(与Queue.get()不同)。管理器可以从队列中删除作业。

那么,worker 如何在不从队列中删除作业的情况下获得作业呢?也许 getput 可以吗?

管理器如何从队列中删除特定作业?

A typical producer-consumer problem is solved in python like below:

from queue import Queue
job_queue = Queue(maxsize=10)    

def manager():
    while i_have_some_job_do:
        job = get_data_from_somewhere()
        job_queue.put(job) #blocks only if queue is currently full

def worker():
    while True:
        data = job_queue.get() # blocks until data available
        #get things done

But I have a variant of producer/consumer problem (not one strictly speaking, so let me call it manager-worker):

The manager puts some job in a Queue, and the worker should keep getting the jobs and doing them. But when the worker get a job, it does not remove the job from the Queue(unlike Queue.get()). And it is the manager which is able to remove a job from the Queue.

So how does the worker get the job while not removing the job from the queue? Maybe get and put is OK?

How does the manager remove a particular job from the queue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只等公子 2025-01-11 00:30:24

也许您的作品无法完全删除作业,但请考虑让它们将它们从原始队列移动到不同的“作业完成”队列。移动本身应该便宜且快速,然后管理器可以处理“工作完成”队列,删除它同意完成的元素,并将其他元素移回工作队列。

Perhaps your works can't remove jobs completely, but consider letting them move them from the original queue to a different "job done" queue. The move itself should be cheap and fast, and the manager can then process the "job done" queue, removing elements it agrees are done, and moving others back to the worker queue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文