python高效编程技巧11(如何使用多线程)
#!/usr/bin/env python# -*- coding:utf-8 -*-# 使用线程的两种方式from threading import Thread# ======1、直接使用Thread======def say_hello(number):print numberprint "hello world"t = Thread(target=say_he
·
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 使用线程的两种方式
from threading import Thread
# ======1、直接使用Thread======
def say_hello(number):
print number
print "hello world"
t = Thread(target=say_hello, args=(1,))
t.start()
print "this is the main thread"
# ======2、使用类来实现多线程======
class myThread(Thread):
def __init__(self, name):
# 要调用分类的构造器
Thread.__init__(self)
self.name = name
def run(self):
print "执行了方法。。。。"
my_thread = myThread("Tom")
my_thread.start()
# 如果my_thread使用了my_thread方法,表示这个这里会阻塞,直到my_thread线程运行完以后,才会执行一下的逻辑
my_thread.join()
print "这是主线程"
更多推荐
所有评论(0)