博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
吴裕雄--天生自然 PYTHON3开发学习:多线程
阅读量:5228 次
发布时间:2019-06-14

本文共 3255 字,大约阅读时间需要 10 分钟。

import _threadimport 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", 4, ) )except:   print ("Error: 无法启动线程")while 1:   pass
import threadingimport timeexitFlag = 0class 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 ("开始线程:" + self.name)        print_time(self.name, self.counter, 5)        print ("退出线程:" + self.name)def print_time(threadName, delay, counter):    while counter:        if exitFlag:            threadName.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()thread1.join()thread2.join()print ("退出主线程")
import threadingimport timeclass 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 ("开启线程: " + 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 -= 1threadLock = 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 ("退出主线程")
import queueimport threadingimport timeexitFlag = 0class 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 ("开启线程:" + self.name)        process_data(self.name, self.q)        print ("退出线程:" + 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 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 ("退出主线程")

 

转载于:https://www.cnblogs.com/tszr/p/10965753.html

你可能感兴趣的文章
监控工具之---Prometheus 安装详解(三)
查看>>
不错的MVC文章
查看>>
IOS Google语音识别更新啦!!!
查看>>
[置顶] Linux终端中使用上一命令减少键盘输入
查看>>
BootScrap
查看>>
路冉的JavaScript学习笔记-2015年1月23日
查看>>
Mysql出现(10061)错误提示的暴力解决办法
查看>>
2018-2019-2 网络对抗技术 20165202 Exp3 免杀原理与实践
查看>>
NPM慢怎么办 - nrm切换资源镜像
查看>>
Swift - UIView的常用属性和常用方法总结
查看>>
Swift - 异步加载各网站的favicon图标,并在单元格中显示
查看>>
【Python学习笔记】1.基础知识
查看>>
梦断代码阅读笔记02
查看>>
selenium学习中遇到的问题
查看>>
大数据学习之一——了解简单概念
查看>>
Linux升级内核教程(CentOS7)
查看>>
Lintcode: Partition Array
查看>>
类别的三个作用
查看>>
Maximum Product Subarray
查看>>
[转载] MySQL的四种事务隔离级别
查看>>