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

  1. Python: 下载底层由Python2转Python3环境更新手记

    谨记录运行环境改变过程中所碰到的坑. 下载底层运行环境由Python2移至Python3所遇到的问题及处理方法: 1.所引的第三方组件,基本都有替代支持:msvcr90.dll不再需要,有则报错2.引 ...

  2. sublime text build system automatic ctrl/cmd+B自动选择 python2 或 python3

    背景 我同时安装了 python2 和 python3 时,python 指向 python2,python3 才是 python3 默认情况下,在 Sublime 内 Ctrl/Cmd + B 运行 ...

  3. python2 与 python3 urllib的互相对应关系

    urllib Python2 name Python3 nameurllib.urlopen() Deprecated. See urllib.request.urlopen() which mirr ...

  4. 在同一台电脑上同时安装Python2和Python3

    目前Python的两个版本Python2和Python3同时存在,且这两个版本同时在更新与维护. 到底是选择Python2还是选择Python3,取决于当前要使用的库.框架支持哪个版本. 例如:HTM ...

  5. python2与python3在windows下共存

    python有python2(工业版)和python3,有时候我们会希望电脑上既有python2也有python3,!假设我们已经安装好,python2和python3了, 接下来我们找到python ...

  6. Python2.7<-------->Python3.x

    版本差异 from __future__   Python2.7 Python3.x 除法 / // Unicode u''                                       ...

  7. 同时使用python2和Python3

    问题:thrift生成的是python2代码,之前使用的是Python3因此需要同时使用两个版本. 方案:将python3的可执行文件重命名为python3(默认为Python),这样使用pyhton ...

  8. python2 到 python3 转换工具 2to3

    windows系统下的使用方法: (1)将python安装包下的Tools/Scripts下面的2to3.py拷贝到需要转换文件目录中. (2)dos切换到需要转换的文件目录下,运行命令2to3.py ...

  9. windows下同时安装python2与python3

    由于python2与python3并不相互兼容,并且差别较大,所以有时需要同时安装,但在操作命令行时,怎么区别python2与python3呢? 1.下载并安装Python 2.7.9和Python ...

随机推荐

  1. Electron-vue项目使用 Inno Setup 创建安装包

    1.安装 Inno Setup 官网:https://jrsoftware.org/isinfo.php 2.打开 Inno Setup ,点击如下图Compli32.exe(首次安装默认打开) 3. ...

  2. Android学习记录(一)——安装Android Studio

    "工欲善其事必先利其器"学习安卓开发的第一步,安装Android Studio. 一.什么是Android Studio? Android Studio 是谷歌推出的一个Andro ...

  3. 有备无患「GitHub 热点速览 v.21.38」

    作者:HelloGitHub-小鱼干 数据库最重要的一个功能是容灾备份,备份不只是对数据库重要,对日常工作生活的我们一样重要,比如花了一个工作日写的代码没有备份(虽然可能只有 1 行-)总归是一个让人 ...

  4. Shell系列(16)- 环境变量配置文件简介及source命令

    变量类型 用户自定义变量(本地变量) 环境变量 预定义变量 位置参数变量 source命令 [root@localhost ~]# source 配置文件 或 [root@localhost ~]# ...

  5. ubuntu中如何切换普通用户、root用户

    1.打开Ubuntu,输入命令:su root,回车提示输入密码,输入密码后提示:认证失败. 2.给root用户设置密码: 命令:sudo passwd root 输入密码,并确认密码. 3.重新输入 ...

  6. pycharm向GitHub提交代码

    设置为自动add commit代码 push代码 查看github,看到提交记录

  7. django forms的常用命令及方法(二)

    根据别人网上发布,个人爱好收集 1.创建Form类 from django.forms import Form from django.forms import widgets from django ...

  8. AT1983-[AGC001E]BBQ Hard【dp,组合数学】

    正题 题目链接:https://www.luogu.com.cn/problem/AT1983 题目大意 给出\(n\)个数对\((a_i,b_i)\) 求 \[\sum_{i=1}^n\sum_{j ...

  9. bootstrap inputfile 使用-上传,回显

    近期用bootstrap 做前端的上传,功能涉及到上传时就是召网上的教程随便弄一搜一大把,但是做到修改页面时候不知道页面该如何回显,折腾了一阵子才完成遂记录下来希望能给看到的小伙伴有点启发吧. 首先是 ...

  10. 一个故事,一段代码告诉你如何使用不同语言(Golang&C#)提供相同的能力基于Consul做服务注册与发现

    目录 引言 什么是微服务 传统服务 微服务 什么是服务注册与服务发现 为什么要使用不同的语言提供相同的服务能力 服务协调器 服务注册 Golang C#(.NetCore3.1) 服务发现 通过Htt ...