以多线程的方式向标准输出打印日志

#!/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 模块来实现多线程的更多相关文章

  1. python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)

    今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系 ...

  2. Python学习笔记- Python threading模块

    Python threading模块 直接调用 # !/usr/bin/env python # -*- coding:utf-8 -*- import threading import time d ...

  3. python threading模块中对于信号的抓取

    最近的物联网智能网关(树莓派)项目中遇到这样一个问题:要从多个底层串口读取发来的数据,并且做出相应的处理,对于每个串口的数据的读取我能想到的可以采用两种方式: 一种是采用轮询串口的方式,例如每3s向每 ...

  4. Python——threading模块(线程)

    一.threading模块的对象 Thread:表示一个执行线程的对象 Lock:锁 Rlock:可重入锁对象 Condition:条件变量对象,使得一个线程等待另一个线程满足特定的“条件” Even ...

  5. python threading模块2

    Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法:另一种是创建一个threading.Thread对 ...

  6. python利用requests和threading模块,实现多线程爬取电影天堂最新电影信息。

    利用爬到的数据,基于Django搭建的一个最新电影信息网站: n1celll.xyz  (用的花生壳动态域名解析,服务器在自己的电脑上,纯属自娱自乐哈.) 今天想利用所学知识来爬取电影天堂所有最新电影 ...

  7. python——threading模块

    一.什么是线程 线程是操作系统能够进行运算调度的最小单位.进程被包含在进程中,是进程中实际处理单位.一条线程就是一堆指令集合. 一条线程是指进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条 ...

  8. python threading模块中的join()方法和setDeamon()方法的一些理解

    之前用多线程的时候看见了很多文章,比较常用的大概就是join()和setDeamon()了. 先说一下自己对join()的理解吧: def join(self, timeout=None): &quo ...

  9. python threading模块

    #coding=utf-8 import threading from time import ctime,sleep def music(func): for i in range(2): prin ...

随机推荐

  1. c++对文件操作的支持(一)

    #include <stdio.h> #include <iostream> #include <fstream> using namespace std; voi ...

  2. Mysql优化之创建高性能索引(一)

    1.索引基础 索引对于良好的性能非常关键.尤其是当表中的数据量越来越大时,索引对性能的影响愈发重要.但是不恰当的索引随着数据量的增加,也会使整个数据库的性能下降. 举个例子: ; 如果在id上建立索引 ...

  3. Gallery过时替代方案HorizontalScrollView

    布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:androi ...

  4. px,dp,dip,sp,in,mm,pt详细分析

    px,dp,dip,sp,in,mm,pt详细分析 px   :(pixels),屏幕的像素点,不同的设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多. dip  :(devi ...

  5. DataSet 中的数据排序 及 DataRow装成DataTable

    1.DataSet 中的数据排序 DataSet ds = new DataSet(); // 获取当前排口的数据 ds = _xiaobill.GetHistoryData(yinZiBianm, ...

  6. 转载【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析  原文链接 http://www.cnblogs.com/monodin/p/3866441.html 1.使用场景 ViewPager+ ...

  7. 去掉ExpandableListView的箭头图标

    到ExpandableListView时有个箭头图标系统自带的在你自定义布局也不能去掉只要设置一个属性即可,如下: settingLists.setGroupIndicator(null);  ~~~ ...

  8. css 优先级

    css优先级的四大原则: 原则一: 继承不如指定 如果某样式是继承来的永远不如具体指定的优先级高.例子1:CODE:<style type="text/css"> &l ...

  9. LINUX总结第13篇:LINUX下动态库及版本号控制

    感觉讲得挺详细 注: ln 命令用法 ln –s 源文件 目标文件 (目标文件即为软链接文件) 可用ls -l查看软链接文件具体指向哪个文件 目录[-] 1. File libhello.c 2. F ...

  10. 图片拉伸: stretchableImageWithLeftCapWidth

    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight: (NSInteger)topCa ...