http://www.cnblogs.com/someoneHan/p/6204640.html 线程一中对threading线程的开启调用做了简单的介绍

1 在线程开始之后,线程开始独立的运行直到目标函数返回为止。如果需要在主线程中判断开启的线程是否在运行可以使用 is_alive()方法:

 import threading
import time def countdown(n):
while n > 0:
print('T-minus', n)
n -= 1
time.sleep(5) t = threading.Thread(target=countdown, args=(10, ))
t.start() if t.is_alive():
print('still alive')
else:
print('complete')

使用is_alive() 判断线程是否使完成状态

2. 还可以使用join链接到当前线程上,那么当前线程会登台线程完成之后在运行

 import threading
import time def countdown(n):
while n > 0:
print('T-minus', n)
n -= 1
time.sleep(2) t = threading.Thread(target=countdown, args=(10, ))
t.start()
t.join()
print('complete')

这样的代码在t线程运行结束之后才会继续运行print函数

3. 守护线程:使用threading.Thread创建出来的线程在主线程退出之后不会自动退出。如果想在主线程退出的时候创建的线程也随之退出

 import threading
import time def countdown(n):
while n > 0:
print('T-minus', n)
n -= 1
time.sleep(2) t = threading.Thread(target=countdown, args=(10, ), daemon=True)
t.start()
print('complete')

运行结果为:

T-minus 10
complete

另外一种设置为守护线程的方式为调用setDaemon()方法。

 t = threading.Thread(target=countdown, args=(10, ))
t.setDaemon(daemonic=True)
t.start()

python 线程之 threading(二)的更多相关文章

  1. python 线程之 threading(三)

    python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...

  2. python 线程之 threading(四)

    python 线程之 threading(三) http://www.cnblogs.com/someoneHan/p/6213100.html中对Event做了简单的介绍. 但是如果线程打算一遍一遍 ...

  3. python 线程之threading(五)

    在学习了Event和Condition两个线程同步工具之后还有一个我认为比较鸡肋的工具 semaphores 1. 使用semaphores的使用效果和Condition的notify方法的效果基本相 ...

  4. python 线程之 threading(一)

    threading:基于对象和类的较高层面上的接口,threading模块在内部使用_thread模块来实现线程的对象以及常用的同步化工具的功能. 使用定制类的方式继承 threading.Threa ...

  5. python 线程之_thread

    python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...

  6. “死锁” 与 python多线程之threading模块下的锁机制

    一:死锁 在死锁之前需要先了解的概念是“可抢占资源”与“不可抢占资源”[此处的资源可以是硬件设备也可以是一组信息],因为死锁是与不可抢占资源有关的. 可抢占资源:可以从拥有他的进程中抢占而不会发生副作 ...

  7. python多线程之threading模块

    threading模块中的对象 其中除了Thread对象以外,还有许多跟同步相关的对象 threading模块支持守护线程的机制 Thread对象 直接调用法 import threading imp ...

  8. python多线程之threading、ThreadPoolExecutor.map

    背景: 某个应用场景需要从数据库中取出几十万的数据时,需要对每个数据进行相应的操作.逐个数据处理过慢,于是考虑对数据进行分段线程处理: 方法一:使用threading模块 代码: # -*- codi ...

  9. python多线程之Threading

    什么是线程? 线程是操作系统内核调度的基本单位,一个进程中包含一个或多个线程,同一个进程内的多个线程资源共享,线程相比进程是“轻”量级的任务,内核进行调度时效率更高. 多线程有什么优势? 多线程可以实 ...

随机推荐

  1. DropDownList控件

    1.DropDownList控件 <asp:DropDownList runat="server" ID="DropDownList1" AutoPost ...

  2. 总结的JS数据类型判定(非常全面)

    用typeof 来检测数据类型 Javascript自带两套类型:基本数据类型(undefined,string,null,boolean,function,object)和对象类型. 但是如果尝试用 ...

  3. Html特殊字符表

    原始字符 entity 原始字符 entity " " & & ' ' < < > >     ¡ ¡ ¢ ¢ £ £ ¤ ¤ ¥ ¥ ¦ ...

  4. Day4-python基础之函数

    本次学习内容: 字典查询快的原因 字符编码 函数定义 局部变量.全局变量 返回值 嵌套函数 递归(二分查找) 三元运算 map lamba 函数式编程 高阶函数 内置函数 字典查询快的原因: 字典占用 ...

  5. 第六章 大数据,6.3 突破传统,4k大屏的沉浸式体验(作者: 彦川、小丛)

    6.3 突破传统,4k大屏的沉浸式体验 前言 能够在 4K 的页面上表演,对设计师和前端开发来说,即是机会也是挑战,我们可以有更大的空间设计宏观的场景,炫酷的转场,让观众感受影院式视觉体验,但是,又必 ...

  6. ubuntu一些基本软件安装方法

    ubuntu一些基本软件安装方法 首先说明一下 ubuntu 的软件安装大概有几种方式:1. deb 包的安装方式deb 是 debian 系 Linux 的包管理方式, ubuntu 是属于 deb ...

  7. jquery实现 复选框 全选

    $("#checkAll").change(function () { $(this).closest("table") .find(":checkb ...

  8. 利用Render Texture实现游戏的小雷达效果(摄影机分屏)

    最近游戏蛮牛在举办一个活动,就是要做出这样的效果: 题目:实现游戏分屏效果 要求:1.        分屏,且分割线不规则(即非水平或垂直):2.        各屏可单独操作(移动.缩放),操作指该 ...

  9. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  10. Spring面试题

    69道Spring面试题和答案 原文地址    译者:深海(1422207401@qq.com)  校对:方腾飞 目录 Spring 概述 依赖注入 Spring beans Spring注解 Spr ...