【发布时间】:2015-09-17 14:36:06
【问题描述】:
最近一直在研究Python的多处理能力,遇到如下代码的问题
import syslog
from multiprocessing import Pool
def launcher(i):
time.sleep(i)
syslog.openlog( 'test', 0, syslog.LOG_LOCAL4 )
syslog.syslog( '{} {}'.format(i,datetime.now()))
if __name__ == '__main__':
pool=Pool(8)
pool.map(launcher,range(1,3000))
pool.close()
pool.join()
其背后的想法很简单:我需要在我的系统日志中获得稳定的消息流(每秒一条消息),但我想通过多处理池在 8 个工作进程中生成它。
在我的系统日志(我的 Ubuntu 上的本地 /var/log/syslog)中,我有以下内容
Sep 17 17:17:57 test: 1 2015-09-17 17:17:57.225699
Sep 17 17:17:58 test: 2 2015-09-17 17:17:58.226957
Sep 17 17:18:00 test: 3 2015-09-17 17:18:00.229196
Sep 17 17:18:03 test: 4 2015-09-17 17:18:03.232390
Sep 17 17:18:07 test: 5 2015-09-17 17:18:07.236587
Sep 17 17:18:12 test: 6 2015-09-17 17:18:12.241737
Sep 17 17:18:18 test: 7 2015-09-17 17:18:18.247926
Sep 17 17:18:25 test: 8 2015-09-17 17:18:25.255169
Sep 17 17:18:29 test: 9 2015-09-17 17:18:29.258229
Sep 17 17:18:33 test: 10 2015-09-17 17:18:33.263454
Sep 17 17:18:42 test: 64 2015-09-17 17:18:42.272675
Sep 17 17:18:52 test: 33 2015-09-17 17:18:52.283012
Sep 17 17:19:01 test: 11 2015-09-17 17:19:01.290070
Sep 17 17:19:02 test: 12 2015-09-17 17:19:02.259826
一是流动不均匀,二是乱序。
如果是这样,可能是什么原因?
为什么 linux 进程调度器与 Python 多处理一样工作?
有什么办法可以通过多处理来解决我的任务吗?
【问题讨论】:
标签: python multiprocessing python-multiprocessing