前言
多线程的好处应该不用多说吧?python语言内置了多线程功能支持,而不是单纯地作为底层操作系统的调度方式,从而简化了 Python 的多线程编程。
在实际应用,多线程还是很有用的,比如有时候可以同时下载多张图片,服务器响应多个请求啥的等等....还有很多实用东西
首先导入库
import threading
import time
类继承创建
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print "Starting " + self.name
# 获得锁,成功获得锁定后返回True
# 可选的timeout参数不填时将一直阻塞直到获得锁定
# 否则超时后将返回False
threadLock.acquire()
print_time(self.name, self.counter, 3)
# 释放锁
threadLock.release()
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print "%s: %s" % (threadName, time.ctime(time.time()))
counter -= 1
threadLock = threading.Lock()
threads = []
创建新线程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
开启新线程
thread1.start()
thread2.start()
添加线程到线程列表
threads.append(thread1)
threads.append(thread2)
等待所有线程完成
for t in threads:
t.join()
print("退出主线程")
感觉如何呀?虽然简单,但是还能实现的,欢迎大家交流,共同学习