我不知道我的线程池执行器中发生了什么

2024-05-20 22:16:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个自动化,通过其API,使用concurrent.futures.ThreadPoolExecutor异步注册Zabbix中的主机,正如您在这段代码中看到的:

def zabbix_create_host(name, ip):
    session = get_session()
    token = get_token()
    print("     "+name+" "+ip)

    headers = {'Content-type': 'application/json'}
    data = '{"jsonrpc": "2.0","method": "host.create","params": {"host": "'+name+'","interfaces": [{"type": 2,"main": 1,"useip": 1,"ip": "'+ip+'","dns": "","port": "10050"}],"groups": [{"groupid": "5"}],"templates": [],"macros": [{"macro": "{$USER_ID}","value": "123321"}],"inventory_mode": 0,"inventory": {"macaddress_a": "01234","macaddress_b": "56768"}},"auth": "'+token+'","id": 1}'
    response = requests.post('http://127.0.0.1/api_jsonrpc.php', headers=headers, data=data)
    response = response.json()

def zabbix_create_host_multiple(names, ips):
    with concurrent.futures.ThreadPoolExecutor(max_workers=12) as executor:
        df = pd.concat(executor,map(zabbix_create_host, names, ips))
    return df

NAMES = ['PC1', 'PC2', 'PC3']
IPS = ['1.1.1.1', '2.2.2.2', '3.3.3.3']

zabbix_create_host_multiple(NAMES, IPS)

但在执行代码时,我得到以下错误:

  File "main.py", line 159, in zabbix_create_hosts
    zabbix_create_host_multiple(names, ips)
  File "main.py", line 173, in zabbix_create_host_multiple
    df = pd.concat(executor,map(zabbix_create_host, names, ips))
  File "/home/sacarino/anaconda3/envs/aZabbix/lib/python3.8/site-packages/pandas/core/reshape/concat.py", line 274, in concat
    op = _Concatenator(
  File "/home/sacarino/anaconda3/envs/aZabbix/lib/python3.8/site-packages/pandas/core/reshape/concat.py", line 328, in __init__
    objs = list(objs)
TypeError: 'ThreadPoolExecutor' object is not iterable

看看你能不能帮我,这是我第一次用这个


Tags: nameinpyiptokenhostnamescreate
1条回答
网友
1楼 · 发布于 2024-05-20 22:16:55
def zabbix_create_host(name, ip):
    session = get_session()
    token = get_token()
    print("     "+name+" "+ip)

    headers = {'Content-type': 'application/json'}
    data = '{"jsonrpc": "2.0","method": "host.create","params": {"host": "'+name+'","interfaces": [{"type": 2,"main": 1,"useip": 1,"ip": "'+ip+'","dns": "","port": "10050"}],"groups": [{"groupid": "5"}],"templates": [],"macros": [{"macro": "{$USER_ID}","value": "123321"}],"inventory_mode": 0,"inventory": {"macaddress_a": "01234","macaddress_b": "56768"}},"auth": "'+token+'","id": 1}'
    response = requests.post('http://127.0.0.1/api_jsonrpc.php', headers=headers, data=data)
    response = response.json()
    return pd.Series(response)

def zabbix_create_host_multiple(names, ips):
    df = pd.DataFrame()
    with concurrent.futures.ThreadPoolExecutor(max_workers=12) as executor:
        df = df.append(list(map(zabbix_create_host, names, ips)))
    return df

NAMES = ['PC1', 'PC2', 'PC3']
IPS = ['1.1.1.1', '2.2.2.2', '3.3.3.3']

zabbix_create_host_multiple(NAMES, IPS)

按序列返回响应并将其附加到数据帧

相关问题 更多 >