#!/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. MySQL逻辑控制语句的使用

    一.IF语句     1).   IF(expr1,expr2,expr3) 如果expr1为true则结果为expr2否则为expr3 -->相当于三元运算符                  ...

  2. JQuery iframe宽高度自适应浏览器窗口大小的解决方法

    iframe宽高度自适应浏览器窗口大小的解决方法   by:授客 QQ:1033553122 1.   测试环境 JQuery-3.2.1.min.js 下载地址: https://gitee.com ...

  3. DWR服务器推送技术

    参考博客:https://blog.csdn.net/Marksinoberg/article/details/55505423 下载Dwr.jar文件 到官网http://directwebremo ...

  4. 无限可能,Elasticsearch(一)

    这个世界已然被数据淹没.多年来,我们系统间流转和产生的大量数据已让我们不知所措. 现有的技术都集中在如何解决数据仓库存储以及如何结构化这些数据. 这些看上去都挺美好,直到你实际需要基于这些数据实时做决 ...

  5. 监控ckeditor内容变化,删除编辑器内图片,ueditor同样适用

    let body = document.querySelector("iframe").contentDocument.body; let observer = new Mutat ...

  6. 【Linux命令】ldconfig动态链接库管理命令

    ldconfig动态链接库管理命令,其目的为了让动态链接库为系统所共享. 作用: 默认搜寻/lilb和/usr/lib,以及配置文件/etc/ld.so.conf内所列的目录下的库文件. 搜索出可共享 ...

  7. 05-Django模型(2)

    1.特殊查询 F和Q查询: 之前的查询都是模型对象的属性与常量值比较,两个属性怎么比较呢?使用F查询. F查询语法: from django.db.models import F F('属性名称') ...

  8. Promise如何解决回调地狱

    为什么要有promise:解决(回调地狱)的问题 ### 回调地狱: ```js //跟以前的if条件地狱很像 // if(){ // if(){ // if(){ // } // } //} $.g ...

  9. Java设计模式:Builder(构建器)模式

    概念定义 Builder模式是一步一步创建一个复杂对象的创建型模式.该模式将构建复杂对象的过程和它的部件解耦,使得构建过程和部件的表示隔离开来. 应用场景 对象创建过程比较复杂,或对创建顺序或组合有依 ...

  10. Docker - 卷组管理(三)

    一.不指定宿主机目录 首先运行一个nginx容器 docker run -d --name mynginx -p 8080:80 -v /usr/share/nginx/html nginx --na ...