参考博客:Py西游攻关之多线程(threading模块)

一、并发与并行的区别

并发:交替做不同事的能力
并行:同时做不同事的能力

行话解释:
并发:不同代码块交替执行的性能
并行:不同代码块同时执行的性能

并发就是指代码逻辑上可以并行,有并行的潜力,但是不一定当前是真的以物理并行的方式运行

并发指的是代码的性质,并行指的是物理运行状态

二、线程与进程

什么是线程

  线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务

什么是进程

  进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程中可以启动多个线程

线程与进程的区别

  1. Threads share the address space of the process that created it; processes have their own address space.
  2. Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
  3. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
  4. New threads are easily created; new processes require duplication of the parent process.
  5. Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
  6. Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes.

三、Python中的GIL

参考博客:Python的GIL是什么鬼,多线程性能究竟如何

四、threading模块

import threading
from time import ctime,sleep
import time def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(4)
print("end listening %s"%ctime()) def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(5)
print('end watching %s'%ctime()) threads = []
t1 = threading.Thread(target=music,args=('七里香',))
threads.append(t1)
t2 = threading.Thread(target=move,args=('阿甘正传',))
threads.append(t2) if __name__ == '__main__': for t in threads:
# t.setDaemon(True)
t.start()
# t.join()
# t1.join()
t2.join()########考虑这三种join位置下的结果?
print ("all over %s" %ctime())

五、同步锁(Lock)

import time
import threading def addNum():
global num #在每个线程中都获取这个全局变量
# num-=1 temp=num
# print('--get num:',num )
time.sleep(0.1)
num =temp-1 #对此公共变量进行-1操作 num = 100 #设定一个共享变量
thread_list = []
for i in range(100):
t = threading.Thread(target=addNum)
t.start()
thread_list.append(t) for t in thread_list: #等待所有线程执行完毕
t.join() print('final num:', num )

程序运行结果不是 0

我们可以用加锁进行解决

import time
import threading def addNum():
global num #在每个线程中都获取这个全局变量
# num-=1
   # print('ok')
lock.acquire()
temp=num
time.sleep(0.1)
num =temp-1 #对此公共变量进行-1操作
lock.release() num = 100 #设定一个共享变量
thread_list = []
lock=threading.Lock() for i in range(100):
t = threading.Thread(target=addNum)
t.start()
thread_list.append(t) for t in thread_list: #等待所有线程执行完毕
t.join() print('final num:', num )

锁只锁在    lock.acquire()  与  lock.release() 之间的部分,这里面就相当于串行,也就是其他部分仍可以并发

同步锁与GIL的关系?

GIL是全局锁,保证在同一时刻只有一个线程在CPython解释器中执行,但这个线程可以随时被线程调度器(自己瞎编的)打断,就是说敏感数据无法保证在一个线程处理完后才能被下一个线程处理。

这时便需要同步锁,也就是我们的用户级互斥,通过加锁,可以保证敏感数据在只有被处理完毕后才能被下一个线程进行处理。

最新文章

  1. SQL SERVER 索引之聚集索引和非聚集索引的描述
  2. Longest Common Subsequence (LCS)
  3. P4factory 运行结果展示 basic_routing 以及 ./run_all_tests 的运行结果
  4. 二十四种设计模式:中介者模式(Mediator Pattern)
  5. java_枚举类枚举值
  6. 【原创】MapReduce计数器
  7. SqlServer 允许保存对数据库中表结构的修改
  8. 使用NUnit进行项目的单元测试
  9. HTML之学习笔记(三)文本标签
  10. mysql声明摘要
  11. 在linux下管理iphone
  12. Android-Volley网络通信框架(ImageRequest,ImageLoader,NetWorkImageView)
  13. 【刷题】牛客网看到的鹅厂ML面筋-部分问题RecSys相关
  14. expect 批量自动部署ssh 免密登陆 之 二
  15. vue自定義指令
  16. [Codeforces778E]Selling Numbers
  17. Hadoop 入门
  18. 一起来学习android自定义控件—边缘凹凸的View
  19. LintCode 532: Reverse Pairs
  20. Currying and Uncurrying Js

热门文章

  1. DataTables入门
  2. 0-NULL-nullptr
  3. elasticsearch集群添加节点
  4. mysql定时清理binlog
  5. Eclipse中使用GIT更新项目
  6. HDU 1205 吃糖果(水题)
  7. BZOJ 3336 Black and White (插头DP)
  8. CVX安装使用
  9. 2019-03-18 用Task Schedule定时调用Python脚本
  10. VS2015 C# 编写USB通信上位机时,改变net框架导致DLL调用失败的问题解决方法