#!/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. vscode中js文件失去高亮/没有智能提示

    vscode中js文件失去高亮/没有智能提示 两步: 第一步:基本的语法高亮提示,需要将vetur删掉,然后把vscode的历史记录缓存删掉,重启vscode. 第二步:js的智能提示,使用插件typ ...

  2. angluarjs实现过滤并替换关键字

    html样式 <body ng-app="myapp" ng-controller="myCtrl"> <input type="t ...

  3. gdisk分区及swap分区

    gdisk分区及swap分区 gdisk分区,分区表是GPT,支持更大的分区,128G gdisk分区 一,添加硬盘 二,分区 1,安装gdisk [root@oldboy ~]# yum insta ...

  4. Tornado(1)

    概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...

  5. css 两行、多行超出后用省略号...表示

    .item-name { letter-spacing: ; overflow: hidden; display: -webkit-box; text-overflow: ellipsis; -web ...

  6. vivado2017.1和modelsim10.5联合仿真

    vivado2017.1和modelsim版本 vivado可在xilinx官网查询匹配的modelsim最低版本,下载modelsim前记得选用合适的版本号,如图 查询地址:https://www. ...

  7. LG5202 「USACO2019JAN」Redistricting 动态规划+堆/单调队列优化

    问题描述 LG5202 题解 \[opt[i]=xx+(cnt[i]-cnt[yy]<=0)\] 发现\(cnt[i]-cnt[yy] <= 0\)只能有两种取值 于是直接堆优化即可 \( ...

  8. 数据嵌入js的关系图

    参照echarts官网,改了一下效果图: 数据放在了js里. 代码: <%@ page language="java" contentType="text/html ...

  9. IDEA debug工具使用

    参考:https://www.cnblogs.com/jajian/p/9410844.html

  10. thinkphp5.1单模块设置

    thinkphp5.1单模块 1. // 是否支持多模块'app_multi_module' => false, // 自动搜索控制器'controller_auto_search' => ...