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 ...
随机推荐
- Mac下Sublime Text3配置Python开发环境
设置Sublime Text的语法为python View -> syntax ->python 设置编译环境(默认python版本2.7) Tools -> Build Syste ...
- JSON,XML设计模式详解
JSON在Java中的应用: Json概念: json 是一种轻量级的数据交换格式,采用完全独立于编程语言的文本格式用来存储和表示数据.JSON的语言简洁清晰,广为大众所欢迎,是一种理想的数据交换语言 ...
- PHP中用+号连接数组的结果是?
我们在开发中,有时候会将两个数组合并连接起来,这个时候要注意了,千万不要偷懒直接使用+号哦,为什么呢?我们看看以下代码: $a = [1, 2]; $b = [4, 5, 6]; $c = $a + ...
- TP6 前一个项目可以,第二个项目配置多域名绑定应用一直失效
之前的域名类似: www.a.com test.a.com edu.a.com ... 文件config/app.php中 'domain_bind' => [ 'test' ...
- Postman 如何调试加密接口?
大家好,我是安果! 众所周知,Postman 是一款非常流行且易用的 API 调试工具,在接口调试或测试时经常被使用针对普通 API 接口,我们可以直接在 Postman 中输入 URL.Query ...
- css 圆形脉冲动画
需求: 项目需要在3D场景增加动画按钮,直接添加到场景时 当场景过大的时候 .加载比较麻烦 因在找资料时发现这玩意居然要付费.故做此记录, 效果: 参考: 1.https://www.jiangwei ...
- vue 主次页面区分
1.路由设定,增加meta参数 { path: '/', name: 'Home', component: Home, meta: { index: 0, showFooter: true //由这个 ...
- 华为云计算IE面试笔记-云磁盘和普通磁盘的区别。
1. 定义 云硬盘:一种虚拟块存储服务,主要为ECS和BMS提供块存储空间 普通磁盘:也称本地硬盘,指挂载在计算实例物理机上的本地硬盘 2. 性能 吞吐量具体情况具体分析.(若云磁盘用的SSD本地磁盘 ...
- 请求既有multipartFile,也有实体的解决方案
上回书我们说到,我们在发文件上传请求的时候,携带数据 this.fileData.append('trainName', this.dataModel.trainName); // 添加培训名称 然后 ...
- 用Flask 实现文件服务器(包含docker版本)
最近有了公司局域网内共享axure原型的需求,所以用Flask开发了一款文件上传/查看工具,记录一下其中的问题和解决方案 这个工具参照了一位大神的uploads工具 https://zhuanlan. ...