动态计算要生成的进程数

发布于 12-08 16:02 字数 1009 浏览 1 评论 0原文

我在year_queue中有一个大约15年的列表,我需要每年生成一个进程。但根据我运行代码的服务器,处理器的数量会有所不同。如何根据服务器中处理器的数量动态改变变量 num_processes?

如果我设置 num_processes >处理器数量,它会相应地自动生成吗?当我测试这个时 - 它创建了 15 个进程&在它们之间分配 CPU 功率。我正在寻找一种方法来首先创建“n”个进程,其中 n = 服务器中的处理器数量,然后当每个进程完成时,会生成下一个进程。

for i in range(num_processes):
    worker = ForEachPerson(year_queue, result_queue, i, dict_of_files)
    print "worker spawned for " + str(i)
    worker.start()

results = []
while len(results) < len(years):
    result = result_queue.get()
    results.append(result)

有人有同样的问题吗?


while year_queue.empty() != True:
    for i in range(num_processes):
      worker = ForEachPerson(year_queue, result_queue, i, dict_of_files)
      print "worker spawned for " + str(i)
      worker.start()

    # collect results off the queue
    print "results being collected"
    results = []
    while len(results) < len(num_processes):
      result = result_queue.get()
      results.append(result)

I have a list of about 15 years in the year_queue, I need to spawn one process for each year. But depending on which server I am running the code, the number of processors vary. How do I dynamically vary the variable num_processes depending on the number of processers in the server?

If I set num_processes > number of processers, would it automatically spawn accordingly? When I test this - it creates 15 processes & splits the CPU power between them. I am looking for a way to first create 'n' number of processes, where n = number of processers in the server, and then as each of those processes finish, the next is spawned.

for i in range(num_processes):
    worker = ForEachPerson(year_queue, result_queue, i, dict_of_files)
    print "worker spawned for " + str(i)
    worker.start()

results = []
while len(results) < len(years):
    result = result_queue.get()
    results.append(result)

Anyone had the same issue?


while year_queue.empty() != True:
    for i in range(num_processes):
      worker = ForEachPerson(year_queue, result_queue, i, dict_of_files)
      print "worker spawned for " + str(i)
      worker.start()

    # collect results off the queue
    print "results being collected"
    results = []
    while len(results) < len(num_processes):
      result = result_queue.get()
      results.append(result)

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

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

发布评论

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

评论(2

这样的小城市2024-12-15 16:02:34

使用多处理池。该类会为您完成选择正确数量的进程并运行它们的所有繁琐工作。它也不会为每个任务生成一个新进程,而是在完成后重用进程。

def process_year(year):
    ...
    return result

pool = multiprocessing.Pool()
results = pool.map(process_year, year_queue)

Use a multiprocessing Pool. The class does all the tedious work of selecting the right number of processes and running them for you. It also doesn't spawn a new process for each task, but reuses processes once they're done.

def process_year(year):
    ...
    return result

pool = multiprocessing.Pool()
results = pool.map(process_year, year_queue)
盛装女皇2024-12-15 16:02:34
from multiprocessing import Process, Queue, cpu_count
from Queue import Empty

class ForEachPerson(Process):
    def __init__(self, year_queue, result_queue, i, dict_of_files):
        self.year_queue=year_queue
        self.result_queue=result_queue
        self.i=i
        self.dict_of_files=dict_of_files
        super(ForEachPerson, self).__init__()

    def run(self):
        while True:
            try:
                year=self.year_queue.get()

                ''' Do something '''

                self.result_queue.put(year)
            except Empty:
                self.result_queue.close()
                return

if __name__ == '__main__':
    year_queue=Queue()
    result_queue=Queue()
    dict_of_files={}

    start_year=1996
    num_years=15

    for year in range(start_year, start_year + num_years):
        year_queue.put(year)

    workers=[]
    for i in range(cpu_count()):
        worker = ForEachPerson(year_queue, result_queue, i, dict_of_files)
        print 'worker spawned for', str(i)
        worker.start()
        workers.append(worker)

    results=[]
    while len(results) < num_years:
        try:
            year=result_queue.get()
            results.append(year)
            print 'Result:', year
        except Empty:
            pass

    for worker in workers:
        worker.terminate()
from multiprocessing import Process, Queue, cpu_count
from Queue import Empty

class ForEachPerson(Process):
    def __init__(self, year_queue, result_queue, i, dict_of_files):
        self.year_queue=year_queue
        self.result_queue=result_queue
        self.i=i
        self.dict_of_files=dict_of_files
        super(ForEachPerson, self).__init__()

    def run(self):
        while True:
            try:
                year=self.year_queue.get()

                ''' Do something '''

                self.result_queue.put(year)
            except Empty:
                self.result_queue.close()
                return

if __name__ == '__main__':
    year_queue=Queue()
    result_queue=Queue()
    dict_of_files={}

    start_year=1996
    num_years=15

    for year in range(start_year, start_year + num_years):
        year_queue.put(year)

    workers=[]
    for i in range(cpu_count()):
        worker = ForEachPerson(year_queue, result_queue, i, dict_of_files)
        print 'worker spawned for', str(i)
        worker.start()
        workers.append(worker)

    results=[]
    while len(results) < num_years:
        try:
            year=result_queue.get()
            results.append(year)
            print 'Result:', year
        except Empty:
            pass

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