这篇文章是别人文章的一个观后小结,不是什么原创。

首先第一个例子:

import threading
import time
def worker():
    print "worker"
    time.sleep(1)
    return
for i in xrange(5):
    t = threading.Thread(target=worker)
    t.start()
 
倒数第二行就是对threading模块简单的实例化一下,生成一个名为t的对象,然后调用start方法执行。非常简单
 
 
第二个例子:
import threading
import time
def worker():
    print "test"
    time.sleep(1)
for i in xrange(5):
    t = threading.Thread(target=worker)
    t.start()
print "current has %d threads" % (threading.activeCount() - 1)
 
activeCount方法会返回threading对象的激活线程数。
 
 
第三个例子:
import threading
import time
def worker():
    print "test"
    time.sleep(2)
threads = []
for i in xrange(5):
    t = threading.Thread(target=worker)
    threads.append(t)
    t.start()
for item in threading.enumerate():
    print item
for item in threads:
    print item
 
enumeration方法会枚举threading对象的全部线程
 
 
第四个例子:
import threading
import time
  
def worker():
    time.sleep(3)
    print "worker"
  
t=threading.Thread(target=worker)
t.setDaemon(True)
t.start()
print "haha"
 
setDaemon方法会设置后台进程。
 
恩,以上是threading的几个常用方法,over~
 

threading模块小结的更多相关文章

  1. threading模块、ThreadLocal

    一.threading模块 1.线程对象的创建 1.1 Thread类直接创建 import threading import time def countNum(n): # 定义某个线程要运行的函数 ...

  2. Python:多线程threading模块

    目录 Thread对象 Lock对象 local对象 Thread对象: 多任务可以由多进程完成,也可以由一个进程内的多线程完成.进程是由至少1个线程组成的. threading模块在较低级的模块 _ ...

  3. python 多线程,tthread模块比较底层,而threading模块是对thread做了一些包装,multithreading

    Python多线程详解 2016/05/10 · 基础知识 · 1 评论· 多线程 分享到:20 本文作者: 伯乐在线 - 王海波 .未经作者许可,禁止转载!欢迎加入伯乐在线 专栏作者. 1.多线程的 ...

  4. python成长之路【第十一篇】:网络编程之线程threading模块

    一.threading模块介绍 threading 模块建立在 _thread 模块之上.thread 模块以低级.原始的方式来处理和控制线程,而 threading 模块通过对 thread 进行二 ...

  5. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

  6. Python学习笔记- Python threading模块

    Python threading模块 直接调用 # !/usr/bin/env python # -*- coding:utf-8 -*- import threading import time d ...

  7. threading模块和queue模块实现程序并发功能和消息队列

    简介: 通过三个例子熟悉一下python threading模块和queue模块实现程序并发功能和消息队列. 说明:以下实验基于python2.6 基本概念 什么是进程? 拥有独立的地址空间,内存,数 ...

  8. Python:使用threading模块实现多线程编程

    转:http://blog.csdn.net/bravezhe/article/details/8585437 Python:使用threading模块实现多线程编程一[综述] Python这门解释性 ...

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

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

随机推荐

  1. 摘录和再编:彻底弄懂JS执行机制

    网文: https://juejin.im/post/59e85eebf265da430d571f89 并发模型和事件循环:https://developer.mozilla.org/zh-CN/do ...

  2. 范式及其在mysql数据库设计中的应用

    一.什么是范式 1.1.范式:Normal Format,是离散数学的知识,是为了解决数据的存储与优化而提出来的.要求存储数据后,凡是能够通过关系寻找出来的数据,坚决不再重复存储,终极目标是为了减少数 ...

  3. Winform关于未找到元数据文件.exe和不包含适合于入口点的静态“Main”方法

    在三层架构中ItcastCaterModel项目是被其他项目引用的,所以输出类型为类库.

  4. systemctl用法及其语法

    1.确定是否安装systemd及其版本 # systemctl –version 2.确定systemd和systemctl的二进制文件和库文件的安装位置 # whereis systemd # wh ...

  5. fly.js抛物线连续不断加入购物车

    http://yanshi.sucaihuo.com/jquery/2/298/demo/

  6. RAID的详细配置

    一.RAID 1.RAID机制通过使用多硬盘并行工作的方式来提高硬盘的IO性能 2.RAID分为多种,称之为RAID level,RAID共有7级:RAID0~RAID6 3.常用的RAID级别有:R ...

  7. NVMe概述

    目前企业SSD市场按照接口协议主要分为SATA SSD,PCIe SSD和NVMe SSD,其中SATA SSD沿用了传统的HDD使用的SATA协议,在企业应用和服务器兼容性上具有优势:而PCIe S ...

  8. 1. Django概述

    1.1 设计模型 Django,但它附带了一个你可以用python代码描述数据库布局的对象关系映射器. 数据模型语法提供了许多丰富的方法来展现你的模型——到目前为止,它解决了多年来数据库模式问题. 简 ...

  9. 蓝牙协议分析(3)_BLE协议栈介绍

    1. 前言 通过“蓝牙协议分析(2)_协议架构”的介绍,大家对蓝牙协议栈应该有了简单的了解,但是,肯定还有“似懂非懂.欲说还休”的感觉.有这种感觉太正常了,毕竟蓝牙协议是一个历史悠久又比较庞大的协议, ...

  10. componentWillMount和componentDidMount的区别

    1.componentWillMount  将要装载,在render之前调用: componentDidMount,(装载完成),在render之后调用 2.componentWillMount  每 ...