threading python2 和python3
from __future__ import division
from __future__ import print_function
import threading
balance = 0
def change_it(n):
global balance
balance +=n
balance -=n
def run_thread(n):
for i in range(10000):
change_it(n)
def create_thread():
for i in range(30):
t1 = threading.Thread(target=run_thread,args=(1,))
t2 = threading.Thread(target=run_thread,args=(-1,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)
def _test_thread():
create_thread()
def test():
_test_thread()
test()
输出
Linux-4.15.0-36-generic-x86_64-with-Ubuntu-16.04-xenial
#39~16.04.1-Ubuntu SMP Tue Sep 25 08:59:23 UTC 2018
('64bit', 'ELF')
Python:2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609]
6
6
-1
0
-28
-41
-40
-49
-56
-54
-60
-63
-68
-73
-69
-78
-53
-60
-53
-58
-48
-71
-82
-83
-130
-129
-111
-100
-84
-173
Linux-4.15.0-36-generic-x86_64-with-Ubuntu-16.04-xenial
#39~16.04.1-Ubuntu SMP Tue Sep 25 08:59:23 UTC 2018
('64bit', 'ELF')
Python:3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609]
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
加锁后
from __future__ import division
from __future__ import print_function
import threading
balance = 0
lock = threading.Lock()
def change_it(n):
global balance
balance +=n
balance -=n
def run_thread(n):
for i in range(10000):
lock.acquire()
try:
change_it(n)
finally:
lock.release()
def create_thread():
for i in range(30):
t1 = threading.Thread(target=run_thread,args=(1,))
t2 = threading.Thread(target=run_thread,args=(-1,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)
def _test_thread():
create_thread()
def test():
_test_thread()
test()
输出
Linux-4.15.0-36-generic-x86_64-with-Ubuntu-16.04-xenial
#39~16.04.1-Ubuntu SMP Tue Sep 25 08:59:23 UTC 2018
('64bit', 'ELF')
Python:2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609]
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Linux-4.15.0-36-generic-x86_64-with-Ubuntu-16.04-xenial
#39~16.04.1-Ubuntu SMP Tue Sep 25 08:59:23 UTC 2018
('64bit', 'ELF')
Python:3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609]
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
threading python2 和python3的更多相关文章
- Python: 下载底层由Python2转Python3环境更新手记
谨记录运行环境改变过程中所碰到的坑. 下载底层运行环境由Python2移至Python3所遇到的问题及处理方法: 1.所引的第三方组件,基本都有替代支持:msvcr90.dll不再需要,有则报错2.引 ...
- sublime text build system automatic ctrl/cmd+B自动选择 python2 或 python3
背景 我同时安装了 python2 和 python3 时,python 指向 python2,python3 才是 python3 默认情况下,在 Sublime 内 Ctrl/Cmd + B 运行 ...
- python2 与 python3 urllib的互相对应关系
urllib Python2 name Python3 nameurllib.urlopen() Deprecated. See urllib.request.urlopen() which mirr ...
- 在同一台电脑上同时安装Python2和Python3
目前Python的两个版本Python2和Python3同时存在,且这两个版本同时在更新与维护. 到底是选择Python2还是选择Python3,取决于当前要使用的库.框架支持哪个版本. 例如:HTM ...
- python2与python3在windows下共存
python有python2(工业版)和python3,有时候我们会希望电脑上既有python2也有python3,!假设我们已经安装好,python2和python3了, 接下来我们找到python ...
- Python2.7<-------->Python3.x
版本差异 from __future__ Python2.7 Python3.x 除法 / // Unicode u'' ...
- 同时使用python2和Python3
问题:thrift生成的是python2代码,之前使用的是Python3因此需要同时使用两个版本. 方案:将python3的可执行文件重命名为python3(默认为Python),这样使用pyhton ...
- python2 到 python3 转换工具 2to3
windows系统下的使用方法: (1)将python安装包下的Tools/Scripts下面的2to3.py拷贝到需要转换文件目录中. (2)dos切换到需要转换的文件目录下,运行命令2to3.py ...
- windows下同时安装python2与python3
由于python2与python3并不相互兼容,并且差别较大,所以有时需要同时安装,但在操作命令行时,怎么区别python2与python3呢? 1.下载并安装Python 2.7.9和Python ...
随机推荐
- POJ 2828 Buy Tickets(线段树单点)
https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...
- IDL使用
出错的问题,可能是因为路径,或者没有建立工程文件. 运行IDL并在preferences项里修改设置(如图中红框所示) (IDL 8.4中在) 2, 中文字符显示乱码,改为gb2312
- django使用restframework序列化查询集合(querryset)
第一: pip install djangorestframework 第二: 在setting.py文件中的app添加名为: 'rest_framework', 第三:再项目的APP下面新建名为(可 ...
- AVS 通信模块之AVSConnectionManager
AVSConnectionManager 类为客户端无缝地管理与AVS的连接 功能简介 失败时连接重试 允许后续重新连接 ping管理 AVS服务器断开时周期重连服务器 允许客户端完全启用或禁用连接管 ...
- [转载]Samba 4实现windows匿名访问Linux共享!
SMB(Server Messages Block,信息服务块). 由于NFS(网络文件系统)可以很好的完成Linux与Linux之间的数据共享,因而 Samba较多的用在了Linux与windows ...
- NWERC2020J-Joint Excavation【构造,贪心】
正题 题目链接:https://codeforces.com/gym/103049/problem/J 题目大意 \(n\)个点\(m\)条边的一张无向图,选出一条路径后去掉路径上的点,然后将剩下的点 ...
- CF587F-Duff is Mad【AC自动机,根号分治】
正题 题目链接:https://www.luogu.com.cn/problem/CF587F 题目大意 给出\(n\)个字符串\(s\).\(q\)次询问给出\(l,r,k\)要求输出\(s_{l. ...
- 4.自定义类加载器实现及在tomcat中的应用
了解了类加载器的双亲委派机制, 也知道了双亲委派机制的原理,接下来就是检验我们学习是否扎实了,来自定义一个类加载器 一. 回顾类加载器的原理 还是这张图,类加载器的入口是c++调用java代码创建了J ...
- web_security学习路线
一.了解黑客是如何工作的 1.在虚拟机配置Linux系统 2.漏洞测试工具 3.msf控制台 4.远程工具RATS 5.远程访问计算机 6.白帽 二.技术基础 漏斗扫描工具AWVS AWVS简介 安装 ...
- visualbox安装ubuntu18过程中不能输入name,password,密码,姓名问题的解决办法+一些基本操作
问题描述:visualbox安装ubuntu18,,,,卡在注册处,无法输入姓名,密码-点击无法输入............如下图: 解决办法:更改你的Ubuntu的主板内存,主板内存默认的值 ...