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 线程基础的更多相关文章
- Qt之线程基础
何为线程 线程与并行处理任务息息相关,就像进程一样.那么,线程与进程有什么区别呢?当你在电子表格上进行数据计算的时候,在相同的桌面上可能有一个播放器正在播放你最喜欢的歌曲.这是一个两个进程并行工作的例 ...
- Android多线程研究(1)——线程基础及源代码剖析
从今天起我们来看一下Android中的多线程的知识,Android入门easy,可是要完毕一个完好的产品却不easy,让我们从线程開始一步步深入Android内部. 一.线程基础回想 package ...
- JAVA与多线程开发(线程基础、继承Thread类来定义自己的线程、实现Runnable接口来解决单继承局限性、控制多线程程并发)
实现线程并发有两种方式:1)继承Thread类:2)实现Runnable接口. 线程基础 1)程序.进程.线程:并行.并发. 2)线程生命周期:创建状态(new一个线程对象).就绪状态(调用该对象的s ...
- 【windows核心编程】 第六章 线程基础
Windows核心编程 第六章 线程基础 欢迎转载 转载请注明出处:http://www.cnblogs.com/cuish/p/3145214.html 1. 线程的组成 ① 一个是线程的内核 ...
- C#当中的多线程_线程基础
前言 最近工作不是很忙,想把买了很久了的<C#多线程编程实战>看完,所以索性把每一章的重点记录一下,方便以后回忆. 第1章 线程基础 1.创建一个线程 using System; usin ...
- Qt 线程基础(Thread Basics的翻译,线程的五种使用情况)
Qt 线程基础(QThread.QtConcurrent等) 转载自:http://blog.csdn.net/dbzhang800/article/details/6554104 昨晚看Qt的Man ...
- 线程基础(CLR via C#)
1.线程基础 1.1.线程职责 线程的职责是对CPU进行虚拟化.Windows 为每个进程豆提供了该进程专用的线程(功能相当于一个CPU).应用程序的代码进入死循环,于那个代码关联的进程会&quo ...
- Linux 系统应用编程——线程基础
传统多任务操作系统中一个可以独立调度的任务(或称之为顺序执行流)是一个进程.每个程序加载到内存后只可以唯一地对应创建一个顺序执行流,即传统意义的进程.每个进程的全部系统资源是私有的,如虚拟地址空间,文 ...
- 《CLR via C#》读书笔记 之 线程基础
第二十五章 线程基础 2014-06-28 25.1 Windows为什么要支持线程 25.2 线程开销 25.3 停止疯狂 25.6 CLR线程和Windows线程 25.7 使用专用线程执行异步的 ...
随机推荐
- 类Date
概述: java.util.Date类 表示特定的瞬间,精确到毫秒.毫秒就是千分之一秒.继续查阅API,发现Date拥有多个构造函数,只是部分已经过时,但是其中有未过时的构造函数可以把毫秒值转成日期对 ...
- keras04 - 阿狗阿猫识别 面向对象编程
参考:https://www.bilibili.com/video/av32756783
- 重写URL
更新网页url /***省略部分代码***/ function rewriteUrl(url) { if (url.substr(0, 1) === "/") { url = (r ...
- IIS7下设置https主机名灰色无法修改
打开iis绑定域名时,点击绑定弹出绑定框,在选择类型为“https”的时候,主机名为灰色的无法填写 解决方法: 1. 选择https,在选择相应的SSL证书,点击确定 2. 打开C:\Windows\ ...
- django 之MTV模型
一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model(模型):和数据库相关的,负 ...
- linux基本命令2
目录: 目录: /bin:可执行文件 /sbin:系统文件 /dev:设备文件 命令: mkdir test cd test touch 11.txt vi 11.txt cat 11.txt cp ...
- Java的String和StringBuilder
一.String 1.创建String对象的方法: String s1="haha"; String s2=new String(); String s3=new String(& ...
- vue.js 列表追加项写法
<ul id="app"> <template v-for="site in sites"> <li>{{ site.nam ...
- 2019-04-28 Mybatis generator逆向工程生成的Example代码分析
今天主要对Mybatis generator生成的DAO层等进行分析,讲解Example类的使用和扩展 1.先在数据库建表 CREATE TABLE `department` ( `fid` ) NO ...
- Golang 介绍与安装
1.介绍与安装 Golang 是什么 Go 亦称为 Golang(按照 Rob Pike 说法,语言叫做 Go,Golang 只是官方网站的网址),是由谷歌开发的一个开源的编译型的静态语言. Gola ...