multiprocessing.Process .

编程入门 行业动态 更新时间:2024-10-25 18:27:10
multiprocessing.Process .__ init __(self)做什么?(What does multiprocessing.Process.__init__(self) do?)

我一直在使用这个链接来了解多处理,但我坚持第二个例子:

import multiprocessing import time class Consumer(multiprocessing.Process): def __init__(self, task_queue, result_queue): multiprocessing.Process.__init__(self) self.task_queue = task_queue self.result_queue = result_queue def run(self): proc_name = self.name while True: next_task = self.task_queue.get() if next_task is None: # Poison pill means we should exit print '%s: Exiting' % proc_name break print '%s: %s' % (proc_name, next_task) answer = next_task() self.result_queue.put(answer) return class Task(object): def __init__(self, a, b): self.a = a self.b = b def __call__(self): time.sleep(0.1) # pretend to take some time to do our work return '%s * %s = %s' % (self.a, self.b, self.a * self.b) def __str__(self): return '%s * %s' % (self.a, self.b) if __name__ == '__main__': # Establish communication queues tasks = multiprocessing.Queue() results = multiprocessing.Queue() # Start consumers num_consumers = multiprocessing.cpu_count() * 2 print 'Creating %d consumers' % num_consumers consumers = [ Consumer(tasks, results) for i in xrange(num_consumers) ] for w in consumers: w.start() # Enqueue jobs num_jobs = 10 for i in xrange(num_jobs): tasks.put(Task(i, i)) # Add a poison pill for each consumer for i in xrange(num_consumers): tasks.put(None) # Start printing results while num_jobs: result = results.get() print 'Result:', result num_jobs -= 1

首先,有人可以请准确解释什么是multiprocessing.Process.__init__(self)吗? 另外我还不完全确定队列是如何工作的,我很困惑如何执行Consumer类中的run方法,即使它从未被调用过(至少明确地......)

如果有人可以帮助我通过示例来获得给定的输出,那将非常感激。

I have been using this link to learn about multiprocessing, but I'm stuck on the second example:

import multiprocessing import time class Consumer(multiprocessing.Process): def __init__(self, task_queue, result_queue): multiprocessing.Process.__init__(self) self.task_queue = task_queue self.result_queue = result_queue def run(self): proc_name = self.name while True: next_task = self.task_queue.get() if next_task is None: # Poison pill means we should exit print '%s: Exiting' % proc_name break print '%s: %s' % (proc_name, next_task) answer = next_task() self.result_queue.put(answer) return class Task(object): def __init__(self, a, b): self.a = a self.b = b def __call__(self): time.sleep(0.1) # pretend to take some time to do our work return '%s * %s = %s' % (self.a, self.b, self.a * self.b) def __str__(self): return '%s * %s' % (self.a, self.b) if __name__ == '__main__': # Establish communication queues tasks = multiprocessing.Queue() results = multiprocessing.Queue() # Start consumers num_consumers = multiprocessing.cpu_count() * 2 print 'Creating %d consumers' % num_consumers consumers = [ Consumer(tasks, results) for i in xrange(num_consumers) ] for w in consumers: w.start() # Enqueue jobs num_jobs = 10 for i in xrange(num_jobs): tasks.put(Task(i, i)) # Add a poison pill for each consumer for i in xrange(num_consumers): tasks.put(None) # Start printing results while num_jobs: result = results.get() print 'Result:', result num_jobs -= 1

First, could someone please explain exactly what multiprocessing.Process.__init__(self) does? Also I'm not entirely sure how the queue works, and I'm confused how the run method in the Consumer class is executed even though it is never called (explicitly at least...)

If someone could help me walk through the example to get the given output, it would be greatly appreciated.

最满意答案

创建类的对象时,会自动调用其__init__()方法来初始化它。 在第二个示例中,类Consumer被定义为从multiprocess.Process继承。 Consumer在初始化时所做的第一件事就是初始化它的基本Process ,以确保它已准备好运行。 与许多OO语言不同,基类__init__()函数不会在Python中自动调用。

当在主进程中的Consumer对象上调用start()时,将在新进程中自动调用run()方法。 这是在多处理文档中。

Queue是多个进程通信的方式。 通常,单独的进程无法看到彼此的数据,但Queue允许它们来回传递消息。 在此示例中,主进程将多个任务放入队列,其他每个进程将从该队列中提取任务,执行该任务,并将结果发送回另一个队列。

When an object of a class is created, its __init__() method gets called automatically to initialize it. In the second example, class Consumer is defined as inheriting from multiprocess.Process. The first thing Consumer does in its initialization is to initialize its base Process, to ensure it's ready to run. Unlike many OO languages, base class __init__() functions aren't automatically invoked in Python.

The run() method is automatically called in the new process when start() is called on the Consumer object in the main process. That's in the multiprocessing docs.

The Queue is how multiple processes communicate. In general, separate processes can't see each other's data, but Queue lets them pass messages back and forth. In this example, the main process is putting several tasks into a queue, and each of the other processes will pull a task from that queue, carry it out, and send the result back on another queue.

更多推荐

本文发布于:2023-07-29 20:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319534.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:multiprocessing   Process

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!