python - 如何将我的这段程序改造成多线程?
大家讲道理
大家讲道理 2017-04-18 09:35:55
0
1
393

我通过VMware的sdk pyvmomi来获取VSphere平台虚拟机的信息,但是整体获取非常慢,现在想通过gevent来改造下,但是不知道该如何下手,请大神们指教下,下面是代码:

main程序:

def vm_info_dump(): # Periodic tasks - get all the vms in VCenter and dump to json file for vc in VC_POOL.keys(): conn = VCenterInfo(vc) vm_info_list = conn.get_vc_vm(vc) file_name = vc json.dump( vm_info_list, open(BASE_DIR + '/vmserver/data/{0}.json'.format(file_name), 'w'), ensure_ascii=False, indent=4 )

main调用的类方法

class VCenterInfo(object): def __init__(self, ip, user=USER, passwd=PASSWORD): # init connection self.ip = ip self.user = user self.password = passwd self.si = connect.SmartConnect( host=self.ip, user=self.user, pwd=self.password ) def vms_info(self): # vm obj list atexit.register(connect.Disconnect, self.si) content = self.si.RetrieveContent() container = content.rootFolder VmViewType = [vim.VirtualMachine] recursive = True VmContainerView = content.viewManager.CreateContainerView( container, VmViewType, recursive) vm_children = VmContainerView.view return vm_children def get_vc_vm(self, vc): vms = self.vms_info() data_list = [] for vm in vms: data = {} summary = vm.summary guest = vm.guest config = vm.config runtime = vm.runtime devices = vm.config.hardware.device # if hasattr(summary, 'config') disk_kb = 0 for device in devices: if isinstance(device, vim.vm.device.VirtualDisk): disk_kb_one = device.capacityInKB disk_kb += disk_kb_one total_disk_gb = disk_kb / 1024 / 1024 data['instance_uuid'] = config.instanceUuid data['list_name'] = config.name data['hostname'] = guest.hostName data['ip'] = get_public_ip(vm) data['os'] = guest.guestFamily data['os_version'] = guest.guestFullName if hasattr(config, 'hardware'): data['cpu'] = config.hardware.numCPU data['mem'] = config.hardware.memoryMB / 1024 data['total_hard_disk'] = total_disk_gb data['tools_status'] = guest.toolsStatus data['guest_status'] = guest.guestState data['power_status'] = runtime.powerState data['vc'] = vc data['esxi_host'] = runtime.host.name data['template'] = summary.config.template data_list.append(data) return data_list
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

répondre à tous (1)
Ty80

看了下貌似也就只有get_vc_vm这个方法可以用多线程搞一搞

from multiprocessing import cpu_count from multiprocessing.dummy import Pool ... def get_vc_vm(self, vc): data_list = [] def multi_thread(vm): nonlocal data_list # 也许用不到 data = {} summary = vm.summary guest = vm.guest config = vm.config runtime = vm.runtime devices = vm.config.hardware.device # if hasattr(summary, 'config') disk_kb = 0 for device in devices: if isinstance(device, vim.vm.device.VirtualDisk): disk_kb_one = device.capacityInKB disk_kb += disk_kb_one total_disk_gb = disk_kb / 1024 / 1024 data['instance_uuid'] = config.instanceUuid data['list_name'] = config.name data['hostname'] = guest.hostName data['ip'] = get_public_ip(vm) data['os'] = guest.guestFamily data['os_version'] = guest.guestFullName if hasattr(config, 'hardware'): data['cpu'] = config.hardware.numCPU data['mem'] = config.hardware.memoryMB / 1024 data['total_hard_disk'] = total_disk_gb data['tools_status'] = guest.toolsStatus data['guest_status'] = guest.guestState data['power_status'] = runtime.powerState data['vc'] = vc data['esxi_host'] = runtime.host.name data['template'] = summary.config.template data_list.append(data) pool = Pool(cpu_count()) pool.map(multi_thread, self.vms_info()) pool.close() pool.join() return data_list
    Derniers téléchargements
    Plus>
    effets Web
    Code source du site Web
    Matériel du site Web
    Modèle frontal
    À propos de nous Clause de non-responsabilité Sitemap
    Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!