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 ...
随机推荐
- C# 中 async 和 await 的基本使用
C# 中 async 和 await 的基本使用 前言 经常在 C# 的代码中看到以 Async 结尾的方法,大概知道意为异步方法,但不知道怎么使用,也不知道如何定义. 对于"同步" ...
- 【TP3.2.3】addAll方法的坑
问题:做一个导入Excel到数据库的功能中需要用到addAll功能,但是每次执行到addAll()时都会报错,如下 Insert value list does not match column li ...
- php设计模式--生成器模式
生成器模式 require "D:\\xxx\bild.php"; require "D:\\xxx\cx_bild.php"; require "D ...
- 华为云计算IE面试笔记-云磁盘和普通磁盘的区别。
1. 定义 云硬盘:一种虚拟块存储服务,主要为ECS和BMS提供块存储空间 普通磁盘:也称本地硬盘,指挂载在计算实例物理机上的本地硬盘 2. 性能 吞吐量具体情况具体分析.(若云磁盘用的SSD本地磁盘 ...
- 卧槽,redis分布式如果用不好,坑真多
前言 在分布式系统中,由于redis分布式锁相对于更简单和高效,成为了分布式锁的首先,被我们用到了很多实际业务场景当中. 但不是说用了redis分布式锁,就可以高枕无忧了,如果没有用好或者用对,也会引 ...
- 3-等待线程终止的join方法
等待线程终止的join方法 在项目实践中经常会遇到一个场景,就是需要等待某几件事完成之后才能继续往下执行,比如线程加载资源等等. package com.heiye.learn1; public cl ...
- mybatis 配置问题查找
mybatis配置问题记录 org.apache.ibatis.binding.BindingException: Type interface com.xx.dao.UserDao(自己项目的文件) ...
- 6.堆和GC
一. 堆和GC介绍 1.java堆的特点 <深入理解java虚拟机>是怎么描述java堆的 Java堆(Java Heap)是java虚拟机所管理的内存中最大的一块 java堆被所有线程共 ...
- Markdown Reference
Markdown For Typora Overview Markdown is created by Daring Fireball; the original guideline is here. ...
- 安装早期老版本 Visual Studio
安装早期老版本 Visual Studio https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/