#!/usr/bin/pythonn
# -*- coding: UTF-8 -*-
"""
学习线程 thread
总结:
1. 主线程退出,所有子线程都退出
2. 子线程 能直接读取外部变量
3. thread.start_new_thread 调用后,立即执行并发代码了,不像有些模块,执行完后,要调用start方法才执行并发代码,xxxx.yyyy().start() 使用:
调用 thread.start_new_thread( <func>,(parmas,parmas))
根据上述 第一条 原则,看情况是否在 启动 线程后 ,主线程代码如何处理
"""
import thread
import time
import threading a = 0 # 不同线程共同操作数据
def print_time(threadname,delay):
global a
print "thread %s,%s start" % (threading.currentThread().getName(),threadname)
count = 0
while count <3:
time.sleep(delay)
count +=1
a +=1
print "%d,%s,%s:%s" % (a,threading.currentThread().getName(),threadname,time.ctime(time.time()) ) print "thread %s,%s end" % (threading.currentThread().getName(),threadname) print "main:%s start" % threading.currentThread().getName() try:
thread.start_new_thread(print_time,("thread1",2,))
thread.start_new_thread(print_time,("thread2",3,))
except:
print "Error: unable to start thread"
pass time.sleep(7)
print "main:%s end" % threading.currentThread().getName()

输出:

主线程等所有子线程跑完
time.sleep() Out:
main:MainThread start
thread Dummy-,thread1 start
thread Dummy-,thread2 start
,Dummy-,thread1:Sat Sep ::
,Dummy-,thread2:Sat Sep ::
,Dummy-,thread1:Sat Sep ::
,Dummy-,thread2:Sat Sep ::
,Dummy-,thread1:Sat Sep ::
thread Dummy-,thread1 end
,Dummy-,thread2:Sat Sep ::
thread Dummy-,thread2 end
main:MainThread end ---------------------------------------------------
主线程没有等子线程跑完 ,自己先跑完
time.sleep()
Out:
main:MainThread start
thread Dummy-,thread1 start
thread Dummy-,thread2 start
,Dummy-,thread1:Sat Sep ::
,Dummy-,thread2:Sat Sep ::
,Dummy-,thread1:Sat Sep ::
,Dummy-,thread1:Sat Sep ::
thread Dummy-,thread1 end
,Dummy-,thread2:Sat Sep :: main:MainThread end

[b0024] python 归纳 (十)_线程 _Thread模块的更多相关文章

  1. [b0032] python 归纳 (十七)_线程同步_信号量Semaphore

    代码: # -*- coding: utf-8 -*- """ 多线程并发同步 ,使用信号量threading.Semaphore 逻辑: 多个线程,对同一个共享变量 , ...

  2. [b0026] python 归纳 (十一)_线程_threading.Thread

    总结: 默认父线程跑完,子线程并不会马上退出,不像 thread.start_threadXXXX 父线程跑完了,并没有退出,一直在那里 线程启动速度很快,不占多少开销,不到1毫 秒 代码: # -* ...

  3. Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块

    Python第十五天  datetime模块 time模块   thread模块  threading模块  Queue队列模块  multiprocessing模块  paramiko模块  fab ...

  4. Python第二十四天 binascii模块

    Python第二十四天 binascii模块 binascii用来进行进制和字符串之间的转换 import binascii s = 'abcde' h = binascii.b2a_hex(s) # ...

  5. python系列十二:python3模块

    #!/usr/bin/python # This Python file uses the following encoding: gbk #Python3 模块 '''用 python 解释器来编程 ...

  6. python学习(十五) 内建模块学习

    介绍python的几个內建模块,原文链接 1 python的时间模块datetime 取现在时间 from datetime import datetime now = datetime.now() ...

  7. [b0034] python 归纳 (十九)_线程同步_条件变量

    代码: # -*- coding: utf-8 -*- """ 学习线程同步,使用条件变量 逻辑: 生产消费者模型 一个有3个大小的产品库,一个生产者负责生产,一个消费者 ...

  8. [b0031] python 归纳 (十六)_线程同步_锁

    # -*- coding: utf-8 -*- """ 学习 多线程同步 使用锁 threading.Lock() 逻辑: 2 个线程,操作同一个整型变量,一个加法,另外 ...

  9. [b0030] python 归纳 (十五)_多进程使用Pool

    1 usePool.py #coding: utf-8 """ 学习进程池使用 multiprocessing.Pool 总结: 1. Pool 池用于处理 多进程,并不 ...

随机推荐

  1. Java操作数据库——手动实现数据库连接池

    Java操作数据库——手动实现数据库连接池 摘要:本文主要学习了如何手动实现一个数据库连接池,以及在这基础上的一些改进. 部分内容来自以下博客: https://blog.csdn.net/soonf ...

  2. spring一个标准的xml文件头

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. Implement Property Value Validation in the Application Model 在应用程序模型中实现属性值验证

    In this lesson, you will learn how to check whether or not a property value satisfies a particular r ...

  4. mysql忧化参数

    转自 https://blog.51cto.com/tongcheng/1710265以下参数是在mysql-5.6.27中使用,可能mysql版本不同使用方法不一样1.线程参数innodb_read ...

  5. EM13C添加agent记录两个报错

    错误一:ADF_FACES-60097:For more information, please see the server's error log for an entry beginning w ...

  6. 2019-2020-1 20199305《Linux内核原理与分析》第一周作业

    进入Linux的世界 1.何为Linux? Linux是一个操作系统. 2.Linux的由来 芬兰赫尔辛基大学的研究生 Linus Torvalds接触Unix时认为其费用不友好,决定自己开发一个操作 ...

  7. mysql中group by 使用

    问题描述 我现在需要查询表test,里面需要安装字段a 进行分组.分组之后还有按照b字段最大的.还要查询出字段c. 我先在使用的数据库是mysql8.0 解决 需注意: group by 分组的时候是 ...

  8. promise 和 setTimeout 在任务队列的执行顺序

    setTimeout(() => { console.log() }); const a = new Promise((resolve,reject)=>{ console.log(); ...

  9. python--各种锁机制归纳整理

    Q:为什么要用锁?什么样的业务场景下需要用锁? 就拿之前的工单系统来说,当审批方式为角色组审批时,代表该角色组内任意一人审批即可,这时,该角色组内成员的系统上都是可以显示审批按钮,如果此时A审批员和B ...

  10. Kettle在windows上安装

    Kettle是一款国外开源的ETL工具,纯java编写,可以在Windows.Linux.Unix上运行,数据抽取高效稳定. 因为有个日常提数,工作日每天都要从数据库中提取数据,转换为excel,再以 ...