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

首先第一个例子:

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. python-html基础操作

    介绍: HTML  是网页内容的载体.包括文字,图片,信息等用户浏览的信息CSS   样式是改变内容外观表现.像字体,颜色,背景,边框等JavaScript   是实现网页上的特效效果.如鼠标滑过背景 ...

  2. 3. Longest Substring Without Repeating Characters无重复字符的最长子串

    网址:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 显然采用sliding window滑 ...

  3. Django框架(五)

    九.Django与Ajax 一.Ajax简介 AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语 ...

  4. vue 数组push元素 视图没更新

    Vue 包含一组观察数组的变异方法,所以它们也将会触发视图更新.这些方法如下: push() pop() shift() unshift() splice() sort() reverse() 问题描 ...

  5. day 01 python基础

    1.计算机历史 2.python历史 宏观: python2和python3的区别: python2  源码不标准,混乱,重复代码过多 python3  统一标准,去除重复代码 3.python环境 ...

  6. VS2015在win10上编译的程序不能在Win7上运行的原因

    研究了下,搞懂原理了.是VS 2015 编译的问题,因为我是Win 10 ,所以会用到win 10 的SDK ,这个SDK 依赖了Universal C Runtime ,就是API-MS-CRT-X ...

  7. 用python一起来看流星雨

    源代码如下(遇上篇烟花代码几乎一样,参数值稍微不一样): # -*- coding: utf-8 -*- # Nola import tkinter as tk from PIL import Ima ...

  8. [Codeforces778E]Selling Numbers

    Problem 给一个由问号和数字组成的数字串A(问号表示任一数字). 再给定n个数字Bi,和0~9的数字的价值. F(x)表示x各个位数上的价值和.问A为何值时,sum(F(Bi+A))的值最大为多 ...

  9. Visual Basic 2017 操作Excel和word【2】持续更新……

    1.控制台程序创建Excel,并设置状态栏显示“Hello World”文本 Module Module1 Private exitXL As Boolean = False Dim WithEven ...

  10. bootstrap之编译CSS和Javascript-0基础安装grunt教程

    昨天晚上看到 bootstrap 全局CSS样式中 使用Less 章节中提到的通过grunt重新编译CSS和Javascript文件,对于我这样从未接触过windows cmd node控制台 npm ...