如何将我的这段程序改造成多线程?

发布于 2022-09-03 14:38:55 字数 2809 浏览 43 评论 0

我通过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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

臻嫒无言 2022-09-10 14:38:55

看了下貌似也就只有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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文