我正在使用 Red Hat Satellite API 为 RHEL4、5、6 系统创建组。没有多处理它可以正常工作,但我想加快这个过程,所以想到使用 multiprocessing.Pool 但不知何故得到不正确的值。这是我的代码:
CLIENT = xmlrpclib.Server(SATELLITE_URL, verbose=0)
KEY = CLIENT.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)
def ADD_SYSTEMS(ITEM):
SYSTEMID = ITEM['id']
SYSTEMNAME = ITEM['name']
RUNNINGKERNEL = CLIENT.system.getRunningKernel(KEY,SYSTEMID)
print type(RUNNINGKERNEL), RUNNINGKERNEL, multiprocessing.current_process().name
if RUNNINGKERNEL.startswith("2.6.32"):
CLIENT.systemgroup.addOrRemoveSystems(KEY,"ALL_RHEL6_SYSTEMS",SYSTEMID,True)
elif RUNNINGKERNEL.startswith("2.6.18"):
CLIENT.systemgroup.addOrRemoveSystems(KEY,"ALL_RHEL5_SYSTEMS",SYSTEMID,True)
else:
CLIENT.systemgroup.addOrRemoveSystems(KEY,"ALL_RHEL4_SYSTEMS",SYSTEMID,True)
if __name__ == '__main__':
ACTIVESYSTEMS = CLIENT.system.listActiveSystems(KEY)
PSIZE = multiprocessing.cpu_count()
P = multiprocessing.Pool(processes=PSIZE)
P.map(ADD_SYSTEMS, ACTIVESYSTEMS)
P.close()
P.join()
CLIENT.auth.logout(KEY)
当我运行代码时,出现以下错误,
<type 'str'> 2.6.18-348.12.1.el5 PoolWorker-1
<type 'str'> 2.6.18-348.18.1.el5 PoolWorker-8
<type 'str'> 2.6.18-371.1.2.el5 PoolWorker-9
<type 'str'> 2.6.18-348.16.1.el5 PoolWorker-10
<type 'str'> 2.6.18-348.16.1.el5 PoolWorker-3
<type 'str'> 2.6.9-100.ELsmp PoolWorker-8
<type 'str'> 2.6.18-371.1.2.el5 PoolWorker-4
<type 'str'> 2.6.32-358.23.2.el6.x86_64 PoolWorker-2
<type 'int'> 1 PoolWorker-11
Traceback (most recent call last):
File "./1rhnGroupMaintenance.py", line 42, in <module>
P.map(ADD_SYSTEMS, ACTIVESYSTEMS)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 227, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 528, in get
raise self._value
AttributeError: 'int' object has no attribute 'startswith'
<type 'str'> 2.6.9-100.ELsmp PoolWorker-1
如果我在函数中注释 if..elif..else 块,则 print 显示正确的值。我不知道我在这里做错了什么。请帮忙。