python threading 模块来实现多线程
以多线程的方式向标准输出打印日志
#!/usr/bin/python import time
import threading class PrintThread(threading.Thread):
def __init__(self,threadid,count,mutex):
threading.Thread.__init__(self)
self.threadid=threadid
self.count=count
self.mutex=mutex
def run(self):
with self.mutex:
for item in range(self.count):
time.sleep(0.05)
print 'threadid is {0} this is the count {1}'.format(self.threadid,item) class PrintThread2(threading.Thread):
def __init__(self,threadid,count,mutex):
threading.Thread.__init__(self)
self.threadid=threadid
self.count=count
self.mutex=mutex
def run(self):
for item in range(self.count):
with self.mutex:
time.sleep(0.1)
print 'thread id is {0} this is the count {1}'.format(self.threadid,item) if __name__=="__main__":
threads=[]
stdoutLock=threading.Lock()
for item in range(5):
pt=PrintThread2(item,100,stdoutLock)
threads.append(pt)
pt.start()
for item in threads:
item.join()
print 'this end ...'
python threading 模块来实现多线程的更多相关文章
- python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)
今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系 ...
- Python学习笔记- Python threading模块
Python threading模块 直接调用 # !/usr/bin/env python # -*- coding:utf-8 -*- import threading import time d ...
- python threading模块中对于信号的抓取
最近的物联网智能网关(树莓派)项目中遇到这样一个问题:要从多个底层串口读取发来的数据,并且做出相应的处理,对于每个串口的数据的读取我能想到的可以采用两种方式: 一种是采用轮询串口的方式,例如每3s向每 ...
- Python——threading模块(线程)
一.threading模块的对象 Thread:表示一个执行线程的对象 Lock:锁 Rlock:可重入锁对象 Condition:条件变量对象,使得一个线程等待另一个线程满足特定的“条件” Even ...
- python threading模块2
Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法:另一种是创建一个threading.Thread对 ...
- python利用requests和threading模块,实现多线程爬取电影天堂最新电影信息。
利用爬到的数据,基于Django搭建的一个最新电影信息网站: n1celll.xyz (用的花生壳动态域名解析,服务器在自己的电脑上,纯属自娱自乐哈.) 今天想利用所学知识来爬取电影天堂所有最新电影 ...
- python——threading模块
一.什么是线程 线程是操作系统能够进行运算调度的最小单位.进程被包含在进程中,是进程中实际处理单位.一条线程就是一堆指令集合. 一条线程是指进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条 ...
- python threading模块中的join()方法和setDeamon()方法的一些理解
之前用多线程的时候看见了很多文章,比较常用的大概就是join()和setDeamon()了. 先说一下自己对join()的理解吧: def join(self, timeout=None): &quo ...
- python threading模块
#coding=utf-8 import threading from time import ctime,sleep def music(func): for i in range(2): prin ...
随机推荐
- String, StringBuilder 与StringBuffer的区别与联系
1.区别 (1)String构建的对象不能改变,每次对String进行操作时,如两个String相加,需要新建一个String对象,然后容纳最终的结果. 而StringBuilder与StringBu ...
- LogBoy logo
- yii操作数据库(PDO)
1.数据访问对象(DAO): 执行 SQL 语句 数据库连接建立后,SQL 语句就可以通过使用 [CDbCommand] 执行了.你可以通过使用指定的SQL语句作为参数调用 [CDbConnectio ...
- JavaScript Infinite scroll & Masonry
// infinitescroll() is called on the element that surrounds // the items you will be loading more of ...
- PHP配置xdebug
其实已经做PHP超过2年了,但是今天特别有感触,所以把过程写在这里 环境是win7+apache2.2+php5.3,因为某种原因,必须使用这个版本. 然后就死活配置不出来.apache日志如下: [ ...
- SSAS维度设计中CustomRollupColumn的用法-自定义聚合方式
CustomRollupColumn说明:指定包含多维表达式的列,该表达式可用于聚合特性的度量值.这个属性覆盖给定度量值的AggregateFunction的属性. 解释:通常我们的度量值 ...
- Oracle前10条记录
在Oracle怎样查询表中的top10条记录呢? select * from test where rownum <=10 下面是关于rownum的介绍 ==================== ...
- Matalab之模糊KMeans原理
对Kmeans方法相信大家都会不陌生,这是一种广泛被应用的基于划分的聚类算法.首先对它的核心思想做一个简单的介绍: 算法把n个向量xj(1,2…,n)分为c个组Gi(i=1,2,…,c),并求每组的聚 ...
- codevs1219 骑士游历
题目描述 Description 设有一个n*m的棋盘(2≤n≤50,2≤m≤50),如下图,在棋盘上有一个中国象棋马. 规定: 1)马只能走日字 2)马只能向右跳 问给定起点x1,y1和终点x2,y ...
- HDOJ-1002 A + B Problem II (非负大整数相加)
http://acm.hdu.edu.cn/showproblem.php?pid=1002 输入的数都是正整数,比较好处理,注意进位. //非负大整数加法 # include <stdio.h ...