python 多线程
来源:互联网 发布:遗传算法理解 编辑:程序博客网 时间:2024/05/22 05:04
一个python 多线程的例子
#coding=utf-8#!/usr/bin/pythonimport threadingimport timeimport libvirtclass mig_thread (threading.Thread): #继承父类threading.Thread def __init__(self, domain, conn): threading.Thread.__init__(self) self.domain = domain self.conn = conn def run(self): #把要执行的代码写到run函数里面 线程在创建后会直接运行run函数 #set maximum bandwidth (in MiB/s) print self.domain.migrateSetMaxSpeed(30,0) #设置最大迁移速度 print self.domain.migrateGetMaxSpeed(0) #获取最大迁移速度 start1 = time.time() self.domain.migrate(self.conn,9,'q_new',None,0) end1 = time.time() #get migrate time print "-----------------------" print "migrate time: %s " % str(end1-start1) print "-----------------------"class set_thread(threading.Thread): def __init__(self,domain): threading.Thread.__init__(self) self.domain = domain def run(self): self.domain.migrateSetMaxDowntime(20,0) #设置最长停机时间 print "set downtime end"conn_004 = libvirt.open('qemu+tcp://qn@server004/system') #创建一个连接conn_005 = libvirt.open('qemu+tcp://qn@server005/system') vm_domain = conn_004.lookupByName('q_new') #查找虚拟机# 创建新线程thread1 = mig_thread(vm_domain,conn_005)thread2 = set_thread(vm_domain)# 开启线程,确保thread1先执行thread1.start()time.sleep(2)thread2.start()print "Exiting Main Thread"
0 0
- Python多线程
- Python多线程
- Python多线程
- python多线程
- python 多线程
- python多线程
- python多线程
- python 多线程
- python多线程
- Python多线程
- Python 多线程
- python多线程
- python 多线程
- {python多线程}
- Python 多线程
- Python多线程
- python 多线程
- Python 多线程
- [leetcode][search] Find Minimum in Rotated Sorted Array
- 播放视频代码
- Linux正则表达式-元字符
- Ubuntu+Apache+PHP+Mysql环境搭建(完整版)
- Android StrictMode 检测
- python 多线程
- 数据结构与算法 (二叉搜索树)
- 并查集
- 清华赵骥主任与CIO石海军研讨互联网信息化
- inline内联函数
- webkit开发前的准备工作
- android 调用系统的照相机和图库实例详解
- 五分钟一个设计模式之工厂方法模式
- Python语法实践之初探列表推导与生成器