我有一个大文本文件,我想处理每一行(做一些操作)并将它们存储在数据库中.由于单个简单程序花费的时间太长,我希望它可以通过多个进程或线程来完成.每个线程/进程应从该单个文件中读取不同的数据(不同的行),并对其数据(行)执行一些操作并将它们放入数据库中,以便最终处理完所有数据并进行处理.数据库与我需要的数据一起转储.
但我无法弄清楚如何处理这个问题.
我正在尝试使用标准python文档中的代码片段来学习如何使用多处理模块.代码将粘贴在此消息的末尾.我在Ubuntu 11.04上使用Python 2.7.1在四核机器上(根据系统监视器,由于超线程而给我八个核心)
问题:尽管已启动多个进程,但所有工作负载似乎只安排在一个核心上,接近100%的利用率.有时,所有工作负载都迁移到另一个核心,但工作负载从不在它们之间分配.
任何想法为什么会这样?
最好的祝福,
保罗
#
# Simple example which uses a pool of workers to carry out some tasks.
#
# Notice that the results will probably not come out of the output
# queue in the same in the same order as the corresponding tasks were
# put on the input queue. If it is important to get the results back
# in the original order then consider using `Pool.map()` or
# `Pool.imap()` (which …Run Code Online (Sandbox Code Playgroud)