#!/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. SSM框架之Mybatis(5)数据库连接池及事务

    Mybatis(5)数据库连接池及事务 1.Mybatis连接池 ​ Mybatis 中也有连接池技术,但是它采用的是自己的连接池技术.在 Mybatis 的 SqlMapConfig.xml 配置文 ...

  2. HTTP中的Accept-Encoding、Content-Encoding、Transfer-Encoding、Content-Type

    Accept-Encoding和Content-Encoding Accept-Encoding和Content-Encoding是HTTP中用来对采用何种压缩格式传输正文进行协定的一对header. ...

  3. JS 注释

    JS 注释 JavaScript 注释可用于提高代码的可读性. 单行注释 // 输出标题: document.getElementById("myH1").innerHTML=&q ...

  4. Andorid Studio 新建模拟器无法联网问题

    1.查看自己本机的dns cmd  -> ipconfing /all 2.修改模拟器的dns 跟PC本机一致.  开启模拟器 -> cmd -> adb root  (需要root ...

  5. Thymeleaf常用语法:模板片断

    系统中的很多页面有很多公共内容,例如菜单.页脚等,这些公共内容可以提取放在一个称为“模板片断”的公共页面里面,其它页面可以引用这个 “模板片断”内容. 一.模板片断的定义 可以是html标签,也可以使 ...

  6. ABP入门教程12 - 展示层实现增删改查-脚本

    点这里进入ABP入门教程目录 创建目录 在展示层(即JD.CRS.Web.Mvc)的\wwwroot\view-resources\Views\下新建文件夹Course //用以存放Course相关脚 ...

  7. 23种设计模式之Builder设计模式

    概述 建造者模式(Builder Pattern),是创造性模式之一,Builder 模式的目的则是为了将对象的构建与展示分离.Builder 模式是一步一步创建一个复杂对象的创建型模式,它允许用户在 ...

  8. 在Ubuntu上安装Miniconda

    在Ubuntu上安装Miniconda Anaconda是一个开源的Python包管理器,而Miniconda则是轻量级的Anaconda 下载地址:https://docs.conda.io/en/ ...

  9. Google Admob移动广告快速集成步骤

    Google Admob移动广告快速集成步骤 第一步:引入依赖包 //admob广告 implementation 'com.google.android.gms:play-services-ads: ...

  10. Linux—系统管理

    查看Linux系统版本                                                                    CentOS系统 # centos7查看命 ...