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 ...
随机推荐
- C语言-getopt函数
#include<unistd.h> int getopt(int argc,char *const argv[],const char *optstring); extern char ...
- GDAL 生成shp文件
附件:http://pan.baidu.com/s/1i3GPwrV(C#版GDAL接口.dll) 示例程序: http://pan.baidu.com/s/1jpIKQ (程序是在vs2008 x ...
- Error:Execution failed for task ':app:mergeDebugResources'. > Some file crunching failed, see logs for details
Android Studio 编译中断.... Error:Execution failed for task ':app:mergeDebugResources'. > Some file c ...
- LogBoy logo
- Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRIDE environment variable to “*****”
Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRIDE environment ...
- 从数据库中导出.csv文件
需求: 本次将数据库中的数据导出成.csv文件(office可以打开), //数据的生成,根据你所选中的数据进行生成 //params:$activity_id -> 活动的id //param ...
- Rule Or WorkFlow
The main value of a Workflow engine is that it makes it possible to customize the flows through some ...
- interbase C++Builder 简单例子
interbase C++Builder 的例子,网上找了半天也没找到合适的,下面是一般能搜索到的文章,现在整理下: 下面我以interbase―――C++Builder,介绍一个简单的例子(不过很 ...
- (Inno setup打包)检测系统是否已安装程序,若已安装则弹出卸载提示的代码
原文 http://bbs.itiankong.com/thread-30983-1-5.html 有6天没研究pascal代码了,昨天晚上突然来了灵感,终于解决了苦思冥想好几天没能解决的问题, 因此 ...
- 四大图像库:OpenCV/FreeImage/CImg/CxImage
1.对OpenCV 的印象:功能十分的强大,而且支持目前先进的图像处理技术,体系十分完善,操作手册很详细,手册首先给大家补计算机视觉的知识,几乎涵盖了近10年内的主流算法: 然后将图像格式和矩阵运算, ...