python threading acquire release
线程同步
//test.py
import threading
import time
exitFlag = 0
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, 5)
threadLock.release()
print "Exiting " + self.name
def print_time(threadName, delay, counter):
while counter:
if exitFlag:
#threading.Thread.exit()
return
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()
#exitFlag = 1
threads.append(thread1)
threads.append(thread2)
for t in threads:
t.join()
print "Exiting Main Thread"
//result
# python test.py
Starting Thread-1
Starting Thread-2
Thread-2: Wed Nov 15 18:31:53 2017
Thread-2: Wed Nov 15 18:31:55 2017
Thread-2: Wed Nov 15 18:31:57 2017
Thread-2: Wed Nov 15 18:31:59 2017
Thread-2: Wed Nov 15 18:32:01 2017
Exiting Thread-2
Thread-1: Wed Nov 15 18:32:02 2017
Thread-1: Wed Nov 15 18:32:03 2017
Thread-1: Wed Nov 15 18:32:04 2017
Thread-1: Wed Nov 15 18:32:05 2017
Thread-1: Wed Nov 15 18:32:06 2017
Exiting Thread-1
Exiting Main Thread
Finally:
看到没有,关键任务是大家排排队,吃果果。
不要争,不要抢,谁先干的,就让他先干完,咋这么过瘾呢!!!:):)
python threading acquire release的更多相关文章
- Python学习笔记- Python threading模块
Python threading模块 直接调用 # !/usr/bin/env python # -*- coding:utf-8 -*- import threading import time d ...
- Python threading(多线程)
threading模块在较低级别thread模块之上构建更高级别的线程接口. 一.threading模块定义了以下函数和对象: threading.active_count() 等同于threadin ...
- python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)
今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系 ...
- python threading.thread
Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法:另一种是创建一个threading.Thread对 ...
- python threading模块的Lock和RLock区别
首先了解这两者是什么. 以下说明参考自python官网 Lock:Lock被称为①原始锁,原始锁是一个②在锁定时不属于特定线程的同步基元组件,它是能用的最低级的同步基元组件.原始锁处于 "锁 ...
- [Python]threading local 线程局部变量小測试
概念 有个概念叫做线程局部变量.一般我们对多线程中的全局变量都会加锁处理,这样的变量是共享变量,每一个线程都能够读写变量,为了保持同步我们会做枷锁处理.可是有些变量初始化以后.我们仅仅想让他们在每一个 ...
- python——threading模块
一.什么是线程 线程是操作系统能够进行运算调度的最小单位.进程被包含在进程中,是进程中实际处理单位.一条线程就是一堆指令集合. 一条线程是指进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条 ...
- python threading编程中的LOCK和RLOCK(可重入锁)
找到一本PYTHON并发编辑的书, 弄弄.. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time sh ...
- python threading基础学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' """ python是支持多线程的,并 ...
随机推荐
- String和datetime在SQL中和在C#中相互转换方法总结
Custom Date and Time Format Strings <= https://docs.microsoft.com/en-us/dotnet/standard/base-ty ...
- poj2728 Desert King【最优比率生成树】【Prim】【0/1分数规划】
含[最小生成树Prim]模板. Prim复杂度为$O(n^2),适用于稠密图,特别是完全图的最小生成树的求解. Desert King Time Limit: 3000MS Memory Li ...
- 闭区间套定理(Nested intervals theorem)讲解2
①确界与极限,看完这篇你才能明白 http://www.cnblogs.com/iMath/p/6265001.html ②这个批注由这个问题而来 表示$c$可能在$\bigcap_{n=1}^{\i ...
- 批量kill java进程方法-引出子shell和反引用
方法: kill –9 `pgrep java` 使用上述命令可以将服务器上运行的所有java进程一次性kill掉. 扩展:子shell和反应用在shell脚本中的作用 先来看一个子shell的例子: ...
- HTML响应状态码
https://www.restapitutorial.com/httpstatuscodes.html
- 查询自己的apple购买历史
https://secure1.store.apple.com/cn/order/list
- confd template src格式和 templates 语法
Template Resources Template resources are written in TOML and define a single template resource. Tem ...
- ionic中数据进行操作后,需要直接显示改变后的数据,数据刷新
数据分页是通过使用下拉加载,查询sqlite本地数据的数据 <ion-refresher (ionRefresh)="doTest($event)"> <ion- ...
- 《mongoDB》索引
一:基础操作 创建单列索引 语法: >db.collection.createIndex(keys, options) 语法中 Key 值为你要创建的索引字段,1 为指定按升序创建索引,如果你想 ...
- Python+Flash+NodeJS 接口自动化平台
一.前端安装步骤# manager-web(1)下载项目 git clone https://github.com/t880216t/manager-web.git (2) 安装依赖 cnpm ins ...