gpt4 book ai didi

python - 多处理 TypeError 'str' 对象不可调用

转载 作者:行者123 更新时间:2023-12-03 12:52:23 27 4
gpt4 key购买 nike

我对 python 的多处理库的了解有限,这可能是一个愚蠢的问题,但在这里。

我正在尝试编写一个程序,该程序利用多处理来保持连接处于事件状态,该连接超时 30 分钟。因此,您必须每 29 分钟接触一次服务器。我的目标是写一个connection_worker,它唯一的工作就是休眠29分钟,然后醒来触摸服务器,然后重新进入休眠状态。我正在使用全局连接对象,因此我不必在工作人员之间传递对象。我只是希望每项工作都能独立完成其所需的工作。工作线程将成为重担,它将在与连接线程不同的 sleep 时间上运行,其 sleep 时间可以从 24 小时到 6 分钟不等。因此,我想将每个进程作为一个使用多处理的进程启动,并让进程访问全局连接变量。

import multiprocessing
import atexit
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import time
from monitor import *
import sys
import datetime
from datetime import timedelta
import time
from decimal import *
import cx_Oracle


global vcenter_connection
current_time = ''

hostname = ''
username = ''
password = ''
vcenter_connection = SmartConnect(host = hostname,user = username,pwd = password)
atexit.register(Disconnect, vcenter_connection)

if __name__ == '__main__':
#worker_1 = multiprocessing.Process(name='do_work', target=do_work())
#connection_service = multiprocessing.Process(name='connection_worker', target=connection_worker())
jobs = ['worker_1()', 'connection_service()']
for i,job in enumerate(jobs):
i = multiprocessing.Process(name=job, target = job)
i.start()

def connection_worker():
while True:
current_time = vcenter_connection.CurrentTime()
print current_time
time.sleep(29*60)

def do_work():
while True:
if vcenter_connection:
print "You still have connection.. Sleeping for 5 minutes"
time.sleep(5*60)
else:
print "no connection"

控制台打印输出:
pydev debugger: starting (pid: 21175)
job
worker_1()
job
connection_service()
Process worker_1():
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
TypeError: 'str' object is not callable
Process connection_service():
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
TypeError: 'str' object is not callable

新代码可以工作,但它会在 time.sleep 上休眠所有运动,我需要连接服务和 do_work 才能协同工作。这样 connection_service 总是在运行。当达到 29 分钟时,我是否应该使用不同的方法进行警报,但是如何让工作线程独立于那个工作线程,是否可以通过多处理独立 sleep ?
import time
from decimal import *
import cx_Oracle


global vcenter_connection
current_time = ''

hostname = ''
username = ''
password = ''
vcenter_connection = SmartConnect(host = hostname,user = username,pwd = password)
atexit.register(Disconnect, vcenter_connection)



def connection_service():
while True:
current_time = vcenter_connection.CurrentTime()
print current_time
time.sleep(29*60)

def do_work():
while True:
if vcenter_connection:
print "You still have connection.. Sleeping for 5 minutes"
time.sleep(5*60)
else:
print "no connection"

if __name__ == '__main__':
#worker_1 = multiprocessing.Process(name='do_work', target=do_work())
#connection_service = multiprocessing.Process(name='connection_worker', target=connection_worker())
jobs = [('worker_1()', do_work), ('connection_service()', connection_service)]
for job in jobs:
i = multiprocessing.Process(name=job[0], target = job[1])
i.start()

最佳答案

你必须通过multiprocessing.Process功能对象。您正在传递字符串:

jobs = ['worker_1()', 'connection_service()']  # Strings.
for i,job in enumerate(jobs):
i = multiprocessing.Process(name=job, target = job)

这应该是:
jobs = [worker_1, connection_service]
for job in jobs:
i = multiprocessing.Process(target=job)

在哪里 worker_1connection_service是在模块顶层定义的函数。

如果要保留 name您当前提供的,您可以使您的列表成为元组列表:
jobs = [('worker_1()', worker_1), ('connection_service()', connection_service)]
for job in jobs:
i = multiprocessing.Process(target=job[1], name=job[0])

关于python - 多处理 TypeError 'str' 对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25187644/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com