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. 管理后台界面 详细分析(内含代码 |【前端】)RuoYi

    最近在做的一个后台管理 因为关于隐私原因 只方便展示个别页面代码 不会上传项目 注意是前端代码 我把项目代码地址放在最后了 如有需要可自取学习   我会为各位兄弟详细的介绍其中各个属性的含义和用法,记 ...

  2. C# 中 AppDomain 的一些理解

    C# 中 AppDomain 的一些理解 前言 一直想写一个这样的程序:与其它的程序完全解耦,但可以动态的加载其它程序,并执行其中的特定方法,执行完后可以卸载,完全不影响该程序本身.最近无意间发现了 ...

  3. [源码解析] 深度学习分布式训练框架 horovod (20) --- Elastic Training Operator

    [源码解析] 深度学习分布式训练框架 horovod (20) --- Elastic Training Operator 目录 [源码解析] 深度学习分布式训练框架 horovod (20) --- ...

  4. contos 7修改root密码

    https://www.linuxidc.com/Linux/2018-01/150211.htm 下面是CentOS 7的root密码修改 开机按esc 选择CentOS Linux (3.10.0 ...

  5. centos 7 & 6 优化脚本

    简单优化 ,未涉及安全优化,如有需求请自行修改脚本实现 1 #!/bin/bash 2 SysVer=`cat /etc/redhat-release | awk -F'release' '{prin ...

  6. ubuntu18.04 kuebadm 安装 k8s-1.15.9

    ubuntu kubeadm 搭建kubernetes1.15.9 准备 update && 安装docker apt-get update apt install docker 修改 ...

  7. 基于AM335X,如何搭建优良的Linux开发环境(下)

    接着上一篇文章的Linux开发环境搭建,文章中详细讲解了 VMware14.1.1虚拟机安装.基于虚拟机安装Ubuntu14.04.3操作系统.安装Ubuntu14.04.3操作系统.安装虚拟机工具. ...

  8. 用Flask 实现文件服务器(包含docker版本)

    最近有了公司局域网内共享axure原型的需求,所以用Flask开发了一款文件上传/查看工具,记录一下其中的问题和解决方案 这个工具参照了一位大神的uploads工具 https://zhuanlan. ...

  9. 11.2.0.4 RAC manual opatch

    1.Stop the CRS managed resources running from DB homes. If this is a GI Home environment, as the dat ...

  10. 【vscode高级玩家】Visual Studio Code❤️安装教程(最新版🎉教程小白也能看懂!)

    目录 如果您在浏览过程中发现文章内容有误,请点此链接查看该文章的完整纯净版 下载 Linux Mac OS 安装 运行安装程序 同意使用协议 选择附加任务 准备安装 开始安装 安装完成 如果您在浏览过 ...