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

时间:2014-08-07 16:29:17

标签: python multithreading multiprocessing

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

我正在尝试编写一个程序,该程序利用多处理来保持连接的活动时间超过30分钟。因此,您必须每29分钟触摸一次服务器。我的目标是编写一个connection_worker,它唯一的工作就是睡眠29分钟,然后醒来并触摸服务器然后再回去睡觉。我使用全局连接对象,所以我不必在工作者之间传递对象。我只是希望每个工作都能完成所需的工作。工作线程将是一个繁重的升降机,它将在不同的睡眠时间内运行,其连接线程的睡眠时间可以从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

新代码可以运行,但它会按时运行所有运动。睡眠,我需要连接服务和do_work一致工作。这样,connection_service始终在运行。我应该在29分钟时使用不同的方法来报警,但是如何独立于那个工作线程睡眠,是否可以通过多重处理独立睡眠?

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()

2 个答案:

答案 0 :(得分:1)

您必须传递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])

答案 1 :(得分:1)

因为multiprocessing.Process()期望function参数为target

if __name__ == '__main__':
    #worker_1 = multiprocessing.Process(name='do_work', target=do_work())
    #connection_service = multiprocessing.Process(name='connection_worker', target=connection_worker())

    # Now jobs contain functions and not strings
    jobs = [worker_1, connection_service]
    for i,job in enumerate(jobs):
         i = multiprocessing.Process(target = job)
         i.start()