2 Replies - 198 Views - Last Post: 07 August 2014 - 03:29 PM Rate Topic: -----

#1 zukeru  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 155
  • Joined: 25-December 09

Python Multiprocessing question.

Posted 07 August 2014 - 09:18 AM

Hello, due to my lack of knowledge as I am just a junior programmer I simply don't know how to do what I want. I want to connection to a vcenter and spawn another process that keeps the connection object alive by calling the server time every 29 minutes, and my worker thread will be doing all the magic with the server i want, because the connection will always be alive.

Heres what I have so far:
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"



But i get the following error:

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



Is This A Good Question/Topic? 0
  • +

Replies To: Python Multiprocessing question.

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3526
  • View blog
  • Posts: 12,034
  • Joined: 12-December 12

Re: Python Multiprocessing question.

Posted 07 August 2014 - 09:30 AM

Which is line 114 in your code? You only have 44 lines.

Quote

TypeError: 'str' object is not callable

This is a common error, and not related to the fact that you are attempting multi-processing. It is something like this:
myString = "hello"
myString("there")     # it is a string, not a function

Was This Post Helpful? 0
  • +
  • -

#3 zukeru  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 155
  • Joined: 25-December 09

Re: Python Multiprocessing question.

Posted 07 August 2014 - 03:29 PM

line 114 is from the python module for multiprocessing ..


The new code works but it sleeps all of the movement on time.sleep, I need both connection service and do_work to work in unison. That way connection_service is always running. Should I use a different method to alarm when 29 minutes is reached, but then how to I sleep the worker thread independently of that one, is it possible to sleep both independently with multiprocessing?

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1