Sushwanth
Sushwanth

Reputation: 625

Run an object method in a daemon thread in python

I am trying to simulate an environment with vms and trying to run an object method in background thread. My code looks like the following.

hyper_v.py file :

import random
from threading import Thread
from virtual_machine import VirtualMachine

class HyperV(object):
def __init__(self, hyperv_name):
    self.hyperv_name = hyperv_name
    self.vms_created = {}

def create_vm(self, vm_name):
    if vm_name not in self.vms_created:
        vm1 = VirtualMachine({'vm_name': vm_name})
        self.vms_created[vm_name] = vm1
        vm1.boot()
    else:
        print('VM:', vm_name, 'already exists')

def get_vm_stats(self, vm_name):
    print('vm stats of ', vm_name)
    print(self.vms_created[vm_name].get_values())

if __name__ == '__main__':
    hv = HyperV('temp')
    vm_name = 'test-vm'
    hv.create_vm(vm_name)
    print('getting vm stats')
    th2 = Thread(name='vm1_stats', target=hv.get_vm_stats(vm_name) )
    th2.start()

virtual_machine.py file in the same directory:

import random, time, uuid, json 
from threading import Thread

class VirtualMachine(object):
def __init__(self, interval = 2, *args, **kwargs):
    self.vm_id = str(uuid.uuid4())
    #self.vm_name = kwargs['vm_name']
    self.cpu_percentage = 0
    self.ram_percentage = 0
    self.disk_percentage = 0
    self.interval = interval

def boot(self):
    print('Bootingup', self.vm_id)
    th = Thread(name='vm1', target=self.update() )
    th.daemon = True      #Setting the thread as daemon thread to run in background
    print(th.isDaemon())  #This prints true 
    th.start()

def update(self):
    # This method needs to run in the background simulating an actual vm with changing values. 
    i = 0 
    while(i < 5 ): #Added counter for debugging, ideally this would be while(True) 
        i+=1 
        time.sleep(self.interval)
        print('updating', self.vm_id)
        self.cpu_percentage = round(random.uniform(0,100),2)
        self.ram_percentage = round(random.uniform(0,100),2)
        self.disk_percentage = round(random.uniform(0,100),2)

def get_values(self):
    return_json = {'cpu_percentage': self.cpu_percentage,
                   'ram_percentage': self.ram_percentage,
                   'disk_percentage': self.disk_percentage}
    return json.dumps(return_json)

The idea is to create a thread that keeps on updating the values and on request, we read the values of the vm object by calling the vm_obj.get_values() we would be creating multiple vm_objects to simulate multiple vms running in parallel and we need to get the information from a particular vm on request.

The problem, that I am facing, is that the update() function of the vm doesnot run in the background (even though the thread is set as daemon thread).

The method call hv.get_vm_stats(vm_name) waits until the completion of vm_object.update() (which is called by vm_object.boot()) and then prints the stats. I would like to get the stats of the vm on request by keeping the vm_object.update() running in the background forever.

Please share your thoughts if I am overlooking anything related to the basics. I tried looking into the issues related to the python threading library but I could not come to any conclusion. Any help is greatly appreciated. The next steps would be to have a REST api to call these functions to get the data of any vm but I am struck with this problem.

Thanks in advance,

Upvotes: 0

Views: 626

Answers (1)

Sushwanth
Sushwanth

Reputation: 625

As pointed out by @Klaus D in the comments, my mistake was using the braces when specifying the target function in the thread definition, which resulted in the function being called right away.

target=self.update() will call the method right away. Remove the () to hand the method over to the thread without calling it.

Upvotes: 1

Related Questions