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 ...
随机推荐
- Wpf TemplateBinding
TemplateBinding. ControlTemplate最终会被用到一个控件上,我们称这个控件为模板目标控件或者模板化控件,ControlTemplate里面的控件可以使用TemplateBi ...
- The listener supports no services
问题描述 sql> lsnrctl start 提示: The listener supports no services The command completed successfully ...
- 设计模式—模板方法(template method)
一.定义 百度百科给的定义:定义一个操作中的算法骨架(稳定),而将一些步骤延迟到子类中(变化).Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 如何做到 ...
- Linux本地yum源配置以及使用yum源安装gcc编译环境
本文档是图文安装本地yum源的教程,以安装gcc编译环境为例. 适用范围:所有的cetos,红帽,fedroa版本 适用人群:有一点linux基础的小白 范例系统版本:CentOS Linux rel ...
- javascript常见问题总结
1.const obj = {a:6}; obj.b=8; obj.a=9;//obj为{a:9,b:8};const定义对象的时候是可以改变内容的. const b = "hello&qu ...
- centos7下安装nginx
1.yum install epel-release(安装epel(Extra Packages for Enterprise Linux)) 2.yum repolist(确保epel添加到yum的 ...
- Python 安装beautifulsoup4遇到No module named setuptools问题解决方法
背景说明: 电脑win7-32 在Python 3.3.5下安装beautifulsoup4 4.6.0(下载链接https://pypi.org/project/beautifulsoup4/#fi ...
- POJ3208 Apocalypse Someday
题意 Language:Default Apocalypse Someday Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 2 ...
- Ubuntu 16下单机安装配置zookeeper和kafka
网上其他的没有一个能直接照做完成的,我这个也是看了些帖子,整出来的怕以后忘记 建议连接工具:Bitvise SSH Client 一.安装配置zookeeper 下载zookeeper 3.4.13: ...
- 【MatConvNet代码解析】 vl_nnsoftmaxloss
背景知识:http://deeplearning.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92 假设softmax层的输入(softmax ...