4

背景
我有一组用于构建和执行 Verilog-AMS 测试台的 Python 脚本。整体设计是在考虑线程的情况下构建的,因为每个主要测试用例都是它自己的测试平台,并且我为每个实例单独提供了所有支持文件/数据输出。唯一共享的项目将是启动器脚本和我的数据提取脚本。我面临的问题是我的 Verilog-AMS 模拟器本身不支持多线程,并且对于我的测试用例,它需要大量时间才能完成。

问题
我正在运行它的机器有 32GiB 的 RAM 和 8 个“内核”可供我使用,我可能能够访问具有 32 个的机器。我想利用可用的计算能力并执行模拟同时。最好的方法是什么?

我目前subprocess.call用来执行我的模拟。我想n一次执行多个命令,每个命令都在单独的线程上执行/作为单独的进程。一旦模拟完成,队列中的下一个(如果存在)将执行。

我对 Python 还很陌生,还没有真正编写过线程应用程序。我想要一些关于我应该如何进行的建议。我看到了这个问题,因此我认为该multiprocessing模块可能更适合我的需求。

大家有什么推荐的?

4

1 回答 1

4

我过去在机器学习和数据挖掘方面做过一些类似的任务。在您的情况下使用multiprocessing可能不是那么困难的任务。这取决于您热衷于制作程序的容忍度,您可以使用线程池模式。我个人最喜欢的是使用生产者-消费者模式Queue,这种设计可以处理各种复杂的任务。这是一个示例玩具程序,使用multiprocessing

import multiprocessing
from multiprocessing import Queue, Process
from Queue import Empty as QueueEmpty

# Assuming this text is very very very very large
text="Here I am writing some nonsense\nBut people will read\n..."

def read(q):
   """Read the text and put in a queue"""
   for line in text.split("\n"):
       q.put(line)

def work(qi, qo):
   """Put the line into the queue out"""
   while True:
        try:
            data = qi.get(timeout = 1) # Timeout after 1 second
            qo.put(data)
        except QueueEmpty:
            return # Exit when all work is done
        except:
            raise # Raise all other errors

def join(q):
    """Join all the output queue and write to a text file"""
    f = open("file.txt", w)
    while True:
         try:
            f.write(q.get(timeout=1))
         except QueueEmpty:
            f.close()
            return
         except:
            raise

def main():
   # Input queue
   qi = Queue()
   # Output queue
   qo = Queue()
   # Start the producer
   Process(target = read, args = (qi, )).start()
   # Start 8 consumers
   for i in range(8):
        Process(target = work, args = (qi, qo, )).start()
   # Final process to handle the queue out
   Process(target = join, args = (qo, )).start()

凭记忆输入,如有错误请指正。:)

于 2013-04-11T05:53:21.717 回答