python2 线程基础
Posted 0点0度
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python2 线程基础相关的知识,希望对你有一定的参考价值。
1,感谢菜鸟教程,
线程基础:导入,创建函数,创建线和运行
import thread import time # 为线程定义一个函数 def print_time(threadName, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print "%s: %s" % (threadName, time.ctime(time.time())) # 创建两个线程 try: thread.start_new_thread(print_time, ("Thread-1", 2,)) #函数名和它的两个参数 thread.start_new_thread(print_time, ("Thread-2", 5,)) except: print "Error: unable to start thread" while 1: pass #两个线程会同时分别进行,一个每五秒打印一次,一个每2秒打印一次
2,threading
import threading import time exitFlag = 0 class myThread(threading.Thread): # 继承父类 threading.Thread def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): # 把要执行的代码写到 run 函数里面 线程在创建后会直接运行 run 函数,所以基本可以理解为threading中,function run里的函数会自动运行 print "Starting " + self.name print_time(self.name, self.counter, 5) print "Exiting " + self.name def print_time(threadName, delay, counter): #这个是单独的一个函数 while counter: if exitFlag: (threading.Thread).exit() time.sleep(delay) print "%s: %s" % (threadName, time.ctime(time.time())) counter -= 1 # 创建新线程 thread1 = myThread(1, "Thread-1", 1) thread2 = myThread(2, "Thread-2", 2) # 开启线程 thread1.start() thread2.start() print "Exiting Main Thread"
3,线程锁
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 # 上锁 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 "Exiting Main Thread"
4,多个线程之间的同步用队列
import Queue import threading import time exitFlag = 0 class myThread(threading.Thread): def __init__(self, threadID, name, q): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.q = q def run(self): print "Starting " + self.name process_data(self.name, self.q) print "Exiting " + self.name def process_data(threadName, q): #实际上就是单个线程轮换着对队列进行某个操作, while not exitFlag: queueLock.acquire() if not workQueue.empty(): data = q.get() queueLock.release() print "%s processing %s" % (threadName, data) else: queueLock.release() time.sleep(1) threadList = ["Thread-1", "Thread-2", "Thread-3"] nameList = ["One", "Two", "Three", "Four", "Five"] queueLock = threading.Lock() workQueue = Queue.Queue(10) threads = [] threadID = 1 # for循环创建新线程 for tName in threadList: thread = myThread(threadID, tName, workQueue) thread.start() threads.append(thread) threadID += 1 # 填充队列 queueLock.acquire() for word in nameList: workQueue.put(word) queueLock.release() # 等待队列清空 while not workQueue.empty(): pass # 通知线程是时候退出 exitFlag = 1 # 等待所有线程完成 for t in threads: t.join() print "Exiting Main Thread"
#实际上就是开三个线程清空了一个队列,线程轮流参与,实现线程之间的同步
以上是关于python2 线程基础的主要内容,如果未能解决你的问题,请参考以下文章
newCacheThreadPool()newFixedThreadPool()newScheduledThreadPool()newSingleThreadExecutor()自定义线程池(代码片段