#!/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. UIView创建xib

    这里有两种类都可以实现,但是推荐用Empty类来创建 (Empty): 参考链接:https://blog.csdn.net/wtdask/article/details/76439295 https ...

  2. pip方式安装Jupyter

    pip方式安装Jupyter 如你的cmd命令窗口无法识别pip命令,请配置下环境变量(将python的''Scripts''文件夹路径添加至''path''变量里面). 使用以下命令更新pip和安装 ...

  3. Java基础之IO技术(一)

    ---恢复内容开始--- Java基础中的IO技术可谓是非常重要,俗话说的好,万丈高楼起于垒土之间.所以学习Java一定要把基础学好,今天我们来学习IO技术的基础. IO无非就是输入与输出,而其中处理 ...

  4. alluxio 安装记录及相关信息

    最近要尝试探究一下alluxio相关的知识,本博客进行对alluxio的安装过程进行备忘: 单例安装过程: https://docs.alluxio.io/os/user/stable/cn/cont ...

  5. May 26th, 2019. Week 22nd, Sunday

    A real loser is somebody that's so afraid of not winning, they don't even try. 真正的失败者,是那些因为害怕不能成功,就连 ...

  6. 面向对象OPP

      在此之前学习的编程方式均称为面向过程,过程类似于函数,只能执行,没有返回值 面向过程和面向对象 面向过程-->怎么做? 面向对象-->谁来做? 相比函数,面向对象 是更大的封装,根据职 ...

  7. nginx学习(三):nginx的进程模型

    概述 nginx 进程分为 master进程和work进程 1.打开配置文件查看,这里我修改为2 [root@xxx conf]# vim nginx.conf #user nobody; worke ...

  8. bzoj5219 [Lydsy2017省队十连测] 最长路径

    题意: 做法来自 首先竞赛图缩点后是一条链,\(1\)号节点在开头的那个\(SCC\)中,因此从\(1\)号节点出发的最长链即为\(1\)号节点所在的\(SCC\)的大小\(+1\)号节点拓扑序之后的 ...

  9. 数位DP入门详解+题目推荐

    \(update:2019-9-6\) 博客里某些东西没有解释清楚,完善了对应的解释 在开始之前,我们先来看一道题--题目链接 题目要求,相邻两位的差大于等于2,那么我们先来构造一个试一试. 比如说\ ...

  10. Java连载39-构造方法详解

    ​一. 1.多行注释:CTRL + shift + / 2.当一个类中没有定义任何构造方法的话,系统默认给该类提供一个无参数的构造方法,这个构造方法被称为缺省构造器. public class D39 ...