【Python】多线程-1
#练习:创建一个线程
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的更多相关文章
- python多线程学习记录
1.多线程的创建 import threading t = t.theading.Thread(target, args--) t.SetDeamon(True)//设置为守护进程 t.start() ...
- python多线程编程
Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...
- Python 多线程教程:并发与并行
转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...
- python多线程
python多线程有两种用法,一种是在函数中使用,一种是放在类中使用 1.在函数中使用 定义空的线程列表 threads=[] 创建线程 t=threading.Thread(target=函数名,a ...
- python 多线程就这么简单(转)
多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...
- python 多线程就这么简单(续)
之前讲了多线程的一篇博客,感觉讲的意犹未尽,其实,多线程非常有意思.因为我们在使用电脑的过程中无时无刻都在多进程和多线程.我们可以接着之前的例子继续讲.请先看我的上一篇博客. python 多线程就这 ...
- python多线程监控指定目录
import win32file import tempfile import threading import win32con import os dirs=["C:\\WINDOWS\ ...
- python多线程ssh爆破
python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...
- 【python,threading】python多线程
使用多线程的方式 1. 函数式:使用threading模块threading.Thread(e.g target name parameters) import time,threading def ...
- <转>Python 多线程的单cpu与cpu上的多线程的区别
你对Python 多线程有所了解的话.那么你对python 多线程在单cpu意义上的多线程与多cpu上的多线程有着本质的区别,如果你对Python 多线程的相关知识想有更多的了解,你就可以浏览我们的文 ...
随机推荐
- leetcode-algorithms-30 Substring with Concatenation of All Words
leetcode-algorithms-30 Substring with Concatenation of All Words You are given a string, s, and a li ...
- 解决jar包依赖:Spring IO platform推出bom
名词解释: BOM(bill of materials):材料清单,用于解决jar包依赖的好方法. Spring IO Platform 缘起:Spring起初只专注ioc和aop,现在已发展成一个庞 ...
- java 中多播、广播编程
在 IP 协议层,有多播.广播的概念.IP 地址分为网络地址和主机地址,主机地址全部为1,就是广播地址.多播使用 D 类地址,从 224.0.0.0 到 239.255.255.255.IP 多播需要 ...
- Qt 设置窗口居中显示和窗体大小
设置窗口居中显示 方法一:在窗口(QWidget类及派生类)的构造函数中添加如下代码: #include <QDesktopWidget> //....... QDesktopWidget ...
- xinetd黑/白名单配置教程(以telnet为例)
对于诸如telnet等托管于xinetd的服务,当请求到来时由于是通过xinetd进行通知,所以可以直接在xinetd上配置白名单允许和拒绝哪些ip连接服务. 本文主要参考xinetd.conf的ma ...
- java继承,final,super,Object类,toString,equals,
Java中的内部类:成员内部类静态内部类方法内部类匿名内部类 内部类的主要作用如下: 1. 内部类提供了更好的封装,可以把内部类隐藏在外部类之内,不允许同一个包中的其他类访问该类 2. 内部类的方法可 ...
- chrome hosts
# Go Hosts# 2017-05-02 02:13:04# Localhost (DO NOT REMOVE)127.0.0.1 localhost::1 localhost ip6 ...
- Mac下安装和配置Maven
1.下载Maven 官网:http://maven.apache.org/download.cgi 下载版本:apache-maven-3.5.3-bin.tar.gz 2.配置环境变量 打开term ...
- 字符串和数组----string
一.初始化string对象的方式 #include <iostream> #include <string> using std::cout; using std::endl; ...
- 逆袭之旅DAY16.东软实训.Oracle.序列
2018-07-12 14:07:44 序列 序列1.创建序列create sequence 序列名 [increment by n] ---步长 [start with n] ---序列的起始值 序 ...