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的更多相关文章

  1. tablib把数据导出为Excel、JSON、CSV等格式的Py库(写入数据并导出exl)

    #tablib把数据导出为Excel.JSON.CSV等格式的Py库 #python 3 import tablib #定义列标题 headers = ('1列', '2列', '3列', '4列', ...

  2. py库: arrow (时间)

    arrow是个时间日期库,简洁易用.支持python3.6 https://arrow.readthedocs.io/en/latest/ arrow官网api https://github.com/ ...

  3. py库: scrapy (深坑未填)

    scrapy 一个快速高级的屏幕爬取及网页采集框架 http://scrapy.org/ 官网 https://docs.scrapy.org/en/latest/ Scrapy1.4文档 http: ...

  4. py库: Tesseract-OCR(图像文字识别)

    http://blog.csdn.net/u012566751/article/details/54094692 Tesseract-OCR入门使用1 http://blog.csdn.net/u01 ...

  5. py库: django (web框架)

    http://www.imooc.com/learn/736 Python-走进Requests库 http://www.imooc.com/learn/790 django入门与实践 http:// ...

  6. python语言线程标准库threading.local源码解读

    本段源码可以学习的地方: 1. 考虑到效率问题,可以通过上下文的机制,在属性被访问的时候临时构建: 2. 可以重写一些魔术方法,比如 __new__ 方法,在调用 object.__new__(cls ...

  7. python 标准库 -- threading

    threading : 提高对网络端口的读写效率. threading.Thread.start() 执行线程操作 threading.Thread.run() 执行线程操作 threading.Th ...

  8. 可以用py库: pyautogui (自动测试模块,模拟鼠标、键盘动作)来代替pyuserinput

    PyAutoGUI 是一个人性化的跨平台 GUI 自动测试模块 pyUserInput模块安装前需要安装pywin32和pyHook模块.(想要装的看https://www.cnblogs.com/m ...

  9. py库: GUI(tkinter)

    图形用户界面(Graphical User Interface,简称 GUI) http://www.runoob.com/python/python-gui-tkinter.html Python ...

随机推荐

  1. ServletContext、ServletRequest和HttpSession的生命周期

    学习web的Listener监听器的时,监听域对象创建的监听器.通过监听器重新分析着三个对象的生命周期,清晰明了. 首先域对象的监听器有三个ServletContextListene.ServletR ...

  2. js复习--基础

    最近工作遇到了一些小困难,基础真的很重要,漫天高楼起于地. 一,script元素 包括type=“text/Javascript”,defer延迟到html加载完解析,src=“../../test. ...

  3. js 实现仿 淘宝 五星评价 demo

    <style> @font-face { font-family: 'iconfont'; /* project id 247957 */ src: url('//at.alicdn.co ...

  4. sed常用操作命令

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据进行替换.删除.新增.选取等特定工作. 命令格式: sed [OPTION]... {script-only-i ...

  5. java_集合类_简

    Collection 来源于Java.util包,实用常用的数据结构,字面意思就是容器 主要方法 boolean add(Object o)添加对象到集合 boolean remove(Object ...

  6. Go语言极速入门手册.go

    Github: https://github.com/coderzh/CodeTips /* gotips_test.go: Golang速学速查速用代码手册 Source: github.com/c ...

  7. c++——智能指针学习(shared_ptr和weak_ptr)

    先看一个例子:Stark和Targaryen家族你中有我,我中有你.我们设计以下类企图避免内存泄漏,使得析构函数都能调用到: #include<iostream> #include< ...

  8. VS 自动展开选中当前代码所在的文件位置的功能

    这个功能相当好,自动在Solution Explorer中展开并定位到当前代码所在的文件,免得自己找位置要找很久. 设置方法: tool>>options>>projects ...

  9. Nginx的编译安装及选项

    编译安装Nginx1.安装常见的工具和库(GCC.PCRE.zlib.OpenSSL) Nginx是一个由C语言编写的,所以需要一个编译工具如GNU的GCC[root@www ~]# yum inst ...

  10. Docker端口映射与容器互联

    Docker提供了两个功能来满足访问的基本需求:一是允许映射容器内应用的服务端口到本地宿主主机:另一个是互联机制实现多个容器间通过容器名来快速实现访问. 一.端口映射实现访问容器 当容器中运行一些网络 ...