py库:threading
https://www.youtube.com/watch?v=DnTn3Yx-Nvg
join功能:
import threading
import time def thread_job2():
print('T2', threading.current_thread()) def thread_job1():
print("-----------T1 begin-----------")
for i in range(10):
print("job2:", threading.current_thread())
time.sleep(0.1)
print("-----------T1 end-----------") def main():
thread1 = threading.Thread(target=thread_job1, name="T1")
thread2 = threading.Thread(target=thread_job2, name="T2")
thread1.start()
thread2.start()
thread1.join() # 要等线程全部运行完,才执行下一步。需要加这一句
print(threading.active_count())
print(threading.enumerate())
print(threading.currentThread())
print("all done") if __name__ == '__main__':
main()
Queue功能
https://www.youtube.com/watch?v=DnTn3Yx-Nvg
https://github.com/MorvanZhou/tutorials/blob/master/threadingTUT/thread4_queue.py 代码
GIL
多线程的运算不一定会效率会提升很多,原因在于 python 的 GIL (global interpreter lock)
https://www.youtube.com/watch?v=2511-7VR4nQ
lock锁
https://www.youtube.com/watch?v=-q4txLdUMBM
https://github.com/MorvanZhou/tutorials/blob/master/threadingTUT/thread6_lock.py
lock和join的区别: lock 是锁住变量的更新,join 是不让主线程比某个 thread 先结束
多进程
多核可以避免上述多线程的劣势
https://morvanzhou.github.io/tutorials/python-basic/multiprocessing/3-queue/
...
py库:threading的更多相关文章
- tablib把数据导出为Excel、JSON、CSV等格式的Py库(写入数据并导出exl)
#tablib把数据导出为Excel.JSON.CSV等格式的Py库 #python 3 import tablib #定义列标题 headers = ('1列', '2列', '3列', '4列', ...
- py库: arrow (时间)
arrow是个时间日期库,简洁易用.支持python3.6 https://arrow.readthedocs.io/en/latest/ arrow官网api https://github.com/ ...
- py库: scrapy (深坑未填)
scrapy 一个快速高级的屏幕爬取及网页采集框架 http://scrapy.org/ 官网 https://docs.scrapy.org/en/latest/ Scrapy1.4文档 http: ...
- py库: Tesseract-OCR(图像文字识别)
http://blog.csdn.net/u012566751/article/details/54094692 Tesseract-OCR入门使用1 http://blog.csdn.net/u01 ...
- py库: django (web框架)
http://www.imooc.com/learn/736 Python-走进Requests库 http://www.imooc.com/learn/790 django入门与实践 http:// ...
- python语言线程标准库threading.local源码解读
本段源码可以学习的地方: 1. 考虑到效率问题,可以通过上下文的机制,在属性被访问的时候临时构建: 2. 可以重写一些魔术方法,比如 __new__ 方法,在调用 object.__new__(cls ...
- python 标准库 -- threading
threading : 提高对网络端口的读写效率. threading.Thread.start() 执行线程操作 threading.Thread.run() 执行线程操作 threading.Th ...
- 可以用py库: pyautogui (自动测试模块,模拟鼠标、键盘动作)来代替pyuserinput
PyAutoGUI 是一个人性化的跨平台 GUI 自动测试模块 pyUserInput模块安装前需要安装pywin32和pyHook模块.(想要装的看https://www.cnblogs.com/m ...
- py库: GUI(tkinter)
图形用户界面(Graphical User Interface,简称 GUI) http://www.runoob.com/python/python-gui-tkinter.html Python ...
随机推荐
- 关于我与小组成员逐步升级C代码时的一些感想【第二次作业】
#include<stdio.h> #include<stdlib.h> #include <time.h> int main(){ srand(time(NULL ...
- 13. Forensics (取证 4个)
Maltego是一个取证和数据挖掘应用程序. 它能够查询各种公共数据源,并以图形方式描绘人员,公司,网站和文档等实体之间的关系. Maltego也是一个开源智能,但不是开源软件. Helix The ...
- BF匹配器
对于BF匹配器,首先我们得用cv2.BFMatcher()创建BF匹配器对象.它取两个可选参数,第一个是normType.它指定要使用的距离量度.默认是cv2.NORM_L2.对于SIFT,SURF很 ...
- 网络请求 Requests
网络请求 Requests url: 就是需要请求,并进行下一步处理的urlcallback: 指定该请求返回的Response,由那个函数来处理.method: 一般不需要指定,使用默认GET方法请 ...
- jumpserver堡垒机部署
初稿(后面我有时间再整理一下,看能不能弄成自动化脚本安装): systemctl stop firewalld #关闭防火墙setenforce 0 #关闭selinuxyum install htt ...
- python selenium Chrome模拟手机浏览器(十七)
在做移动端页面测试时可以利用Chrome mobile emulation 辅助完成页面的适配问题,但是目前手机市场上的型号居多我们也没有办法通过人工的模式一一的去适配,所以这里考虑到通过自动化的模式 ...
- JRockit检测Tomcat内存溢出JAVA内存泄漏问题
http://blog.csdn.net/liyanhui1001/article/details/8240473 公司的一个Java应用系统上线以来,基本每1天OutOfMemoryError: P ...
- HTML与CSS:结构与表现
在HTML和CSS里存在着部分重复的功能,例如两者都可以设定一段文字的字体属性.既然如此,为啥还要CSS呢(至少,为啥CSS里存在着和HTML标签属性重复的东西呢)? 这是因为,HTML和CSS的用途 ...
- [蓝桥杯]PREV-23.历届试题_数字游戏
问题描述 栋栋正在和同学们玩一个数字游戏. 游戏的规则是这样的:栋栋和同学们一共n个人围坐在一圈.栋栋首先说出数字1.接下来,坐在栋栋左手边的同学要说下一个数字2.再下面的一个同学要从上一个同学说的数 ...
- 《剑指offer(第二版)》面试题64——求1+2+...+n
一.题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句 (即三元运算符,A? B : C) 二.题解 虽然求和问 ...