使用 Python 多处理延迟启动任务
Delayed start of tasks with Python multiprocessing
我最近一直在研究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 多进程一样工作?
有什么方法可以完全解决我的多处理任务吗?
即使 OS 只对您的程序进行实时调度,您也不会在每一秒后收到统一的消息:
- 取 8 个厨房定时器和一堆 post-its。
- 数字 2999 post-它的 1 到 2999.
- 拿一个计时器和一个 post-it,将时间设置为 post-it 上的数字,然后放在一边。
- 重复 3 直到 运行 超时
- (如果你的速度非常快,亚秒级速度)你有 8 个计时器从 [1, 2, 3 ,4, 5, 6, 7, 8] 开始倒计时
- 等一下
- 现在您的第一个计时器应该响起,重复第 3 步。
- 你现在有 8 个定时器 运行ning
顺序为
[9, 1, 2, 3, 4, 5, 6, 7]
[8, 10, 1, 2, 3, 4, 5, 6]
[7, 9, 11, 1, 2, 3, 4, 5]
[6, 8, 10, 12, 1, 2, 3, 4]
[5, 7, 9, 11, 13, 1, 2, 3]
[4, 6, 8, 10, 12, 14, 1, 2]
[3, 5, 7, 9, 11, 13, 15, 1]
[2, 4, 6, 8, 10, 12, 14, 16]
#Notice that for the next timer to go off, you have to wait 2 seconds, not 1!
[17, 2, 4, 6, 8, 10, 12, 14]
[15, 18, 2, 4, 6, 8, 10, 12]
...
[3, 6, 9, 12, 15, 18, 21, 24]
#3 seconds to wait, not 1!
[25, 3, 6, 9, 12, 15, 18, 21]
...
[4, 8, 12, 16, 20, 24, 28, 32]
编辑:
我猜为什么您如此快速地经历 1、2、3 的等待可能是因为您的 8 个工作人员同时尝试对同一个系统日志执行 IO。我认为那里有一些阻碍。
我最近一直在研究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 多进程一样工作?
有什么方法可以完全解决我的多处理任务吗?
即使 OS 只对您的程序进行实时调度,您也不会在每一秒后收到统一的消息:
- 取 8 个厨房定时器和一堆 post-its。
- 数字 2999 post-它的 1 到 2999.
- 拿一个计时器和一个 post-it,将时间设置为 post-it 上的数字,然后放在一边。
- 重复 3 直到 运行 超时
- (如果你的速度非常快,亚秒级速度)你有 8 个计时器从 [1, 2, 3 ,4, 5, 6, 7, 8] 开始倒计时
- 等一下
- 现在您的第一个计时器应该响起,重复第 3 步。
- 你现在有 8 个定时器 运行ning
顺序为
[9, 1, 2, 3, 4, 5, 6, 7]
[8, 10, 1, 2, 3, 4, 5, 6]
[7, 9, 11, 1, 2, 3, 4, 5]
[6, 8, 10, 12, 1, 2, 3, 4]
[5, 7, 9, 11, 13, 1, 2, 3]
[4, 6, 8, 10, 12, 14, 1, 2]
[3, 5, 7, 9, 11, 13, 15, 1]
[2, 4, 6, 8, 10, 12, 14, 16]
#Notice that for the next timer to go off, you have to wait 2 seconds, not 1!
[17, 2, 4, 6, 8, 10, 12, 14]
[15, 18, 2, 4, 6, 8, 10, 12]
...
[3, 6, 9, 12, 15, 18, 21, 24]
#3 seconds to wait, not 1!
[25, 3, 6, 9, 12, 15, 18, 21]
...
[4, 8, 12, 16, 20, 24, 28, 32]
编辑:
我猜为什么您如此快速地经历 1、2、3 的等待可能是因为您的 8 个工作人员同时尝试对同一个系统日志执行 IO。我认为那里有一些阻碍。