背景
我有一组用于构建和执行 Verilog-AMS 测试台的 Python 脚本。总体设计是在考虑线程的情况下构建的,因为每个主要测试用例都是它自己的测试台,而且我为每个实例提供了所有支持文件/数据输出。唯一共享的项目是启动器脚本和我的数据提取脚本。我面临的问题是我的 Verilog-AMS 模拟器本身不支持多线程,对于我的测试用例,它需要大量时间才能完成。
问题
我正在运行的机器有 32GiB 的 RAM 和 8 个“内核”可供我使用,我可以访问一台有 32 个内核的机器。我想利用可用的计算能力并同时执行模拟.什么是最好的方法?
我目前使用 subprocess.call
来执行我的模拟。我想一次执行最多 n
命令,每个命令都在单独的线程/作为单独的进程执行。模拟完成后,队列中的下一个(如果存在)将执行。
我是 Python 的新手,还没有真正编写过线程应用程序。我想要一些关于我应该如何进行的建议。我看到了this问题,因此我认为 multiprocessing
模块可能更适合我的需求。
大家都推荐什么?
最佳答案
我过去曾在机器学习和数据挖掘方面完成过一些类似的任务。在您的案例中使用 multiprocessing
可能不是那么困难的任务。这取决于您热衷于制作程序的容忍度,您可以使用线程池 模式。我个人最喜欢的是使用 Queue
的Producer - Consumer 模式,这种设计可以处理各种复杂的任务。这是一个使用 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()
凭内存输入,如有错误请指正。 :)
关于Python - 多线程/多处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15939426/