#练习:创建一个线程
from threading import Thread
import time def run(a = None, b = None) :
print a, b
time.sleep(1) t = Thread(target = run, name="wangjing",args = ("this is a", "thread"))
#此时线程是新建状态 print t.getName() #获得线程对象名称
print t.isAlive() #判断线程是否还活着,在start后,在执行完毕前调用isAlive()才会返回True
t.start() #启动线程
#print t.isAlive()
t.join() #主线程等待子线程t执行结束
print "done!" #练习:用继承的方法创建线程
from threading import Thread
import time class MyThread(Thread) :
def __init__(self, a) :
#调用父类的构造方法
super(MyThread, self).__init__()
self.a = a def run(self) :
time.sleep(self.a)
print "sleep :", self.a t1 = MyThread(2)
t2 = MyThread(4)
#多线程的执行是没有顺序,本例主要是增加了较长的等待时间,才看起来有执行顺序
t1.start()
t2.start()
t1.join()
t2.join()
print "done!" #练习:线程
import threading
import time
class timer(threading.Thread):
#The timer class is derived from the class threading.Thread
def __init__(self, num, interval):
threading.Thread.__init__(self)
self.thread_num = num
self.interval = interval
self.thread_stop = False def run(self):
#Overwrite run() method, put what you want the thread do here
while not self.thread_stop:
print 'Thread Object(%d), Time:%s\n' %(self.thread_num, time.ctime())
time.sleep(self.interval)
def stop(self):
self.thread_stop = True
 
def test():
thread1 = timer(1, 1)
thread2 = timer(2, 2)
thread1.start() #执行类里面的run方法
thread2.start()
time.sleep(10) #这里是主线程等待10秒
thread1.stop()
thread2.stop() #停止线程 #这里的停止是主线程调用的
 
if __name__ == '__main__':
test() #练习:线程中再启动一个线程
import threading
import time
def context(tJoin):
print 'in threadContext.'
tJoin.start()
# 将阻塞tContext直到threadJoin终止
tJoin.join()
# tJoin终止后继续执行
print 'out threadContext.' def join1():
print 'in threadJoin.'
time.sleep(1)
print 'out threadJoin.' if __name__=="__main__":
tJoin = threading.Thread(target = join1)
#将线程对象作为参数传入另一个线程中
tContext = threading.Thread(target = context, args = (tJoin,))
tContext.start() #练习:将子线程设置为守护线程
import threading
import time
class MyThread(threading.Thread):
def __init__(self, id):
threading.Thread.__init__(self) def run(self):
time.sleep(5)
print "This is " + self.getName() if __name__ == "__main__":
t1 = MyThread(999)
#t1.setDaemon(True)
# 将子线程设置为守护线程,设置为守护线程后,主线程退出子线程跟着退出,防止子线程挂起
t1.start()
print "I am the father thread."

【Python】多线程-1的更多相关文章

  1. python多线程学习记录

    1.多线程的创建 import threading t = t.theading.Thread(target, args--) t.SetDeamon(True)//设置为守护进程 t.start() ...

  2. python多线程编程

    Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...

  3. Python 多线程教程:并发与并行

    转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...

  4. python多线程

    python多线程有两种用法,一种是在函数中使用,一种是放在类中使用 1.在函数中使用 定义空的线程列表 threads=[] 创建线程 t=threading.Thread(target=函数名,a ...

  5. python 多线程就这么简单(转)

    多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...

  6. python 多线程就这么简单(续)

    之前讲了多线程的一篇博客,感觉讲的意犹未尽,其实,多线程非常有意思.因为我们在使用电脑的过程中无时无刻都在多进程和多线程.我们可以接着之前的例子继续讲.请先看我的上一篇博客. python 多线程就这 ...

  7. python多线程监控指定目录

    import win32file import tempfile import threading import win32con import os dirs=["C:\\WINDOWS\ ...

  8. python多线程ssh爆破

    python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...

  9. 【python,threading】python多线程

    使用多线程的方式 1.  函数式:使用threading模块threading.Thread(e.g target name parameters) import time,threading def ...

  10. <转>Python 多线程的单cpu与cpu上的多线程的区别

    你对Python 多线程有所了解的话.那么你对python 多线程在单cpu意义上的多线程与多cpu上的多线程有着本质的区别,如果你对Python 多线程的相关知识想有更多的了解,你就可以浏览我们的文 ...

随机推荐

  1. IE的“浏览器模式”和“文档模式的区别”

    1.浏览器模式 用于切换IE针对该网页的默认文档模式.对不同版本浏览器的条件备注解析.发送给网站服务器的用户代理(User_Agent)字符串的值.网站可以根据浏览器返回的不同用户代理字符串判断浏览器 ...

  2. WDA基础七:TABStrip

    这个组件很少用. 一般用这个,是为了页面好看,还有就是布局排版的问题,因为多个页签其实占的是一块地... 新建ELEMENT TABStrip,右键tabstrip,插入页签,需要几个加几个... 加 ...

  3. UI基础一:简单的BOL查询

    利用标准的BOL编辑工具,添加BOL对象,重写查询方法,实现简答的BOL查询 1.SE11创建查询对象结构: 2.SE11创建查询结果对象: 3.SE24新建处理类: 重写查询结果方法: METHOD ...

  4. Event IO Process

    先了解一下process和event loop EventLoop 除了异步Server和Client库之外,Swoole扩展还提供了直接操作底层epoll/kqueue事件循环的接口.可将其他扩展创 ...

  5. noip2014无线网络发射器选址

    题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的 129 条东西向街道和 129 条南北向街道所形成的网格状,并且 ...

  6. iOS runtime实用篇--和常见崩溃say good-bye!

    程序崩溃经历 其实在很早之前就想写这篇文章了,一直拖到现在. 程序崩溃经历1 我们公司做的是股票软件,但集成的是第三方的静态库(我们公司和第三方公司合作,他们提供股票的服务,我们付钱).平时开发测试的 ...

  7. TOR的十个最好的替代工具

    TOR的十个最好的替代工具: 一.Comodo Dragon(基于Chromium) TOR基于Firefox,因为我们换个口味,首先推荐一个基于开源项目Chromium的Comodo Dragon, ...

  8. vue数组操作不更新视图问题

    vue 观察数组的变异方法 更新视图 push() pop() shift() unshift() splice(i,n,arr) sort(xx) reverse() ex: app.book.pu ...

  9. weblogic补丁安装失败(Patch B25A is mutually exclusive and cannot coexist with patch(es): UIAL)

    由于曝出漏洞(CVE-2017-3248)需要将weblogic补丁更新至B25A,但是出现报错.如下: Conflict(s) detected - resolve conflict conditi ...

  10. GNU和GPL的区别/关系

    GUN:GNU's Not UNIX的缩写,是一项运动.是1983年Richard Stallman针对UNIX走向毕源和和收费后发起的运动,旨在打造出一套完全开源免费的操作系统. 为了更好地实施GN ...