0

我正在使用 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 显示正确的值。我不知道我在这里做错了什么。请帮忙。

4

3 回答 3

0

It seems the problem is with the limitation on Red Hat Satellite API calls in version 5.4.1 at one point in time. I didn't find any Red Hat official documentation about this limitation but when I tested with 2 workers and the code worked just fine. I was running the code from my workstation with 12 CPU cores which Satellite API could handle and returned 1 for some of the calls. I will have to hard code the number of worker process.

于 2013-11-13T11:28:49.797 回答
0

看起来像这个远程过程调用

RUNNINGKERNEL = CLIENT.system.getRunningKernel(KEY,SYSTEMID)

有时会返回一个字符串(“kernel something”),并且对于特定的键返回一个 int (1)

尝试startswith在 int 上使用失败,因为它是一个字符串方法

我会检查罪魁祸首上远程过程调用的实际答案,或者如果答案是预期的,请确保您实际处理的是字符串

RUNNINGKERNEL = str(CLIENT.system.getRunningKernel(KEY,SYSTEMID))

或明确处理 int 的情况:

if isinstance(RUNNINGKERNEL,int): pass else isinstance(RUNNINGKERNEL,str): # 你的一堆 if

于 2013-11-13T09:58:38.383 回答
0

正如您在循环中看到的, 的值为RUNNINGKERNEL一种int情况:

<type 'int'> 1 PoolWorker-11

而且您正在尝试使用str.startswith错误的整数。

>>> (1).startswith

Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    (1).startswith
AttributeError: 'int' object has no attribute 'startswith'

要解决此问题,您可以检查类型RUNNINGKERNEL并在此基础上添加一些新条件。

if isinstance(RUNNINGKERNEL, int):
   #do something
elif isinstance(RUNNINGKERNEL, str):
   if RUNNINGKERNEL.startswith("2.6.32"):
      ...
于 2013-11-13T09:43:16.150 回答