一个python 多线程的例子
#coding=utf-8 #!/usr/bin/python import threading import time import libvirt class 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"