Python 线程高级篇 threading 模块的使用
创建一个tread实例,给他传一个函数
#!/usr/bin/python
import threading
from time import *
loops =[4,2]
def loop (nloop ,nsec):
print ('start loop ',nloop,'at:', ctime())
sleep(nsec)
print ('loop', nloop ,'done at:', ctime())
def main():
print ('starting at:',ctime())
threads=[]
nloops=range(len(loops))
for i in nloops:
t=threading.Thread(target=loop,args=(i, loops[i]))
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print ('all done at:',ctime())
if __name__=='__main__':
main()
创建一个tread实例给他传入一个可调用的类的实例
#!/usr/bin/python
import threading
from time import *
loops=[4,2]
class ThreadFunc(object):
def __init__(self,func,args,name=''):
self.name=name
self.func=func
self.args=args
def __call__(self):
self.func(*self.args)
def loop(nloop ,nsec):
print ('start loop ', 'at:',ctime())
sleep(nsec)
print ('loop',nloop,'done at',ctime())
def main():
print ('starting at:',ctime())
threads=[]
nloops=range(len(loops))
for i in nloops:
t=threading.Thread(target=ThreadFunc(loop ,(i,loops[i]),loop.__name__)) #t=threading.Thread(target=loop,args=(i, loops[i]))
threads.append(t)
for i in nloops:
threads[i].start() print ('all done at :',ctime()) if __name__=='__main__':
main()
理解的代码:
loops=[4,2]
from time import *
def loop(nloop ,nsec):
print ('start loop ', 'at:',ctime())
sleep(nsec)
print ('loop',nloop,'done at',ctime()) class ThreadFunc(object): def __init__(self,func,args,name=''):
self.name=name
self.func=func
self.args=args
def __call__(self):
self.func(*self.args) for i in range(2):
target=ThreadFunc(loop ,(i,loops[i]),loop.__name__) #target=(对象 ,一个参数 ,名字)
print (target)
print (target.name)
print (target.func)
#print (target.args)
print (loop.__name__)
target()
print (target()) #t=threading.Thread(target=loop,args=(i,loops[i]))
#t=threading.Thread(target=ThreadFunc(loop ,(i,loops[i]),loop.__name__))
#t=threading.Thread(target=loop,args=(i, loops[i]))
关于__call__(self )的使用
#!/usr/bin/poython
class ybl:
def __init__(self ,name):
self.name=name
def __call__(self):
print (self.name)
b=ybl('zx')
print (b.name)
b()
Python 线程高级篇 threading 模块的使用的更多相关文章
- Python之路(第四十一篇)线程概念、线程背景、线程特点、threading模块、开启线程的方式
一.线程 之前我们已经了解了操作系统中进程的概念,程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是 ...
- 人生苦短之我用Python篇(线程/进程、threading模块:全局解释器锁gil/信号量/Event、)
线程: 有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.是一串指令的集合.线程是程序中一个单一的顺序控制流程.进程内一个相对独立的.可调度的执行单元,是 ...
- python 多线程编程之threading模块(Thread类)创建线程的三种方法
摘录 python核心编程 上节介绍的thread模块,是不支持守护线程的.当主线程退出的时候,所有的子线程都将终止,不管他们是否仍在工作. 本节开始,我们开始介绍python的另外多线程模块thre ...
- “死锁” 与 python多线程之threading模块下的锁机制
一:死锁 在死锁之前需要先了解的概念是“可抢占资源”与“不可抢占资源”[此处的资源可以是硬件设备也可以是一组信息],因为死锁是与不可抢占资源有关的. 可抢占资源:可以从拥有他的进程中抢占而不会发生副作 ...
- python多线程之threading模块
threading模块中的对象 其中除了Thread对象以外,还有许多跟同步相关的对象 threading模块支持守护线程的机制 Thread对象 直接调用法 import threading imp ...
- python利用requests和threading模块,实现多线程爬取电影天堂最新电影信息。
利用爬到的数据,基于Django搭建的一个最新电影信息网站: n1celll.xyz (用的花生壳动态域名解析,服务器在自己的电脑上,纯属自娱自乐哈.) 今天想利用所学知识来爬取电影天堂所有最新电影 ...
- Python之多线程:Threading模块
1.Threading模块提供的类 Thread,Lock,Rlock,Condition,Semaphore,Event,Timer,local 2.threading模块提供的常用的方法 (1)t ...
- Python 学习 第九篇:模块
模块是把程序代码和数据封装的Python文件,也就是说,每一个以扩展名py结尾的Python源代码文件都是一个模块.每一个模块文件就是一个独立的命名空间,用于封装顶层变量名:在一个模块文件的顶层定义的 ...
- 线程高级篇-Lock锁和Condition条件
浅谈Synchronized: synchronized是Java的一个关键字,也就是Java语言内置的特性,如果一个代码块被synchronized修饰了,当一个线程获取了对应的锁,执行代码块时,其 ...
随机推荐
- 作用域与this
面向对象 一.单例模式 1.1 对象数据类型的作用: 把描述一个对象的属性和方法放在一个单独的空间,与其他的对象分割开,即时出现属性名相同的情况,也不会产生冲突 var name="xiao ...
- C#RichTextBox复制并跳转指定行
方法一: rTxt.Focus(); //设置文本框中选定的文本起始点 为 指定行数第一个字符的索引 rTxt.SelectionStart = rTxt.GetFirstCharIndexFromL ...
- 使用xpath进行熟悉href属性
HTML文档 <html> <body> <a href="http://www.example.com">Example</a> ...
- Docker决战到底(三) Rancher2.x的安装与使用 - 简书
原文:Docker决战到底(三) Rancher2.x的安装与使用 - 简书 image.png 当越来越多的容器化应用被部署,一个可以管理编排这些容器的工具此时就显得尤为重要了.目前容器编排领域 ...
- STM32 HAL库利用DMA实现串口不定长度接收方法
参考:https://blog.csdn.net/u014470361/article/details/79206352 我这里使用的芯片是 F1 系列的,主要是利用 DMA 数据传输方式实现的,在配 ...
- 【转】 c#中两个DateTimePicker,一个时间设置为0:0:0,另一个设置为23:59:59
[转] c#中两个DateTimePicker,一个时间设置为0:0:0,另一个设置为23:59:59 stp1为第一个DateTimePicker this.dtp1.Value=this.dtp1 ...
- 对Java、C#转学swift的提醒:学习swift首先要突破心理障碍。
网上非常多都说swift是一门新手友好的语言. 但以我当年从Java转学Ruby的经验,swift对于从Java.C#转来的程序猿实际并不友好.原因就在于原来总有一种错觉:一个语言最重要的就是严谨,而 ...
- ThinkPHP5.0框架开发--第4章 TP5.0路由
ThinkPHP5.0框架开发--第4章 TP5.0路由 第4章 TP5.0 路由 ================================================== 上次复习 1. ...
- SSH Key的生成和使用(for git)
SSH Key的生成和使用 一.总结 1.用git base生成ssh,会生成id_rsa.pub文件,还有一个私钥文件. $ ssh-keygen -t rsa -C “youremailn ...
- 你不知道的JavaScript博文参考书籍
you don't know js系列书籍是谷歌地图开发人员编写,内容非常好,四卷已收集齐全. 笔者打包上传到了CSDN,下载地址: http://download.csdn.net/detail/r ...