[python 学习] python 多线程
1、
# -*- coding: utf-8 -*-
import threading
import time
import random def go(name):
for i in range(2):
integer = random.randint(0,10)
print u'{} 前进{} 步.'.format(name,integer)
time.sleep(2) def back(name):
for j in range(500):
integer = random.randint(0,10)
print u'{} 后退{} 步.'.format(name,integer)
time.sleep(1) if __name__ == '__main__':
t1 = threading.Thread(target=go,args=('go',))
t2 = threading.Thread(target=back,args=('back',))
t1.start()
t2.start()
print 'stop stop stop'

上面的 go() 和 back() 交替休眠,谁获得执行权谁执行。(教随机)
2、经典的生产者消费者,两个线程能够 " 沟通 " 。
# -*- coding: utf-8 -*-
import threading
import time
import random
import sys def product(condition):
global integers
while True:
condition.acquire() #获得锁
print u'狗粮生产机启动.................................'
while integers < 100:
time.sleep(1)
integer = random.randint(0,10) #生产
integers = integers + integer
print u'生产了 {} 颗狗粮,总共 {} 颗狗粮'.format(integer,integers)
if integer in [6,8]:
print u'上次生产了 {} 颗狗粮,狗粮生产机要休息休息了,唤醒单身去吃狗粮'.format(integer)
break
print u'狗粮生产机停止................................'
condition.notify() #通知
#condition.wait()
condition.release() #释放锁 def customer(condition):
global integers
while True:
condition.acquire() #获得锁
while integers > 0:
time.sleep(1)
integer = random.randint(0,10)
integers_tmp = integers - integer
if integers_tmp < 0:
integer = integers
integers = 0
print u'单身狗吃 {} 颗狗粮,没有狗粮,汪汪汪...'.format(integer)
else:
integers = integers - integer
print u'单身狗吃 {} 颗狗粮,还有 {} 狗粮.'.format(integer,integers)
condition.notify() #通知
#condition.wait()
condition.release()#释放锁 if __name__ == '__main__':
integers = 0
condition = threading.Condition()
t1 = threading.Thread(target=product,args=(condition,))
t2 = threading.Thread(target=customer,args=(condition,))
t1.start()
t2.start()

上面的狗粮生产机(生产者)和单身狗(消费者),一个生产狗粮,一个吃狗粮。
[python 学习] python 多线程的更多相关文章
- 1 python学习——python环境配置
1 python学习--python环境配置 要学习python语言,光看书看教程还是不好,得动手去写.当然,不管学习什么编程语言,最佳的方式还在于实践. 要实践,先得有一个Python解释器来解释执 ...
- Python学习---Python安装与基础1205
1.0. 安装 1.1.1. 下载 官网下载地址:https://www.python.org/downloads/release/python-352/ 1.1.2. 配置环境变量 因为在安装的时候 ...
- Python学习---Python下[元组]的学习
元组是不可变的, 用小括号()定义,而且一旦定义 ,不可变[类型是tuple] [元组看做一个整体,不可拆分,不可赋值,但可以全部重新赋值] 通过圆括号,用逗号分隔,常用在使语句或用户定义的函数能够安 ...
- Python学习---Python的异步IO[all]
1.1.1. 前期环境准备和基础知识 安装: pip3 install aiohttp pip3 install grequests pip3 install wheel pip3 install s ...
- Python学习--Python基础语法
第一个Python程序 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码. linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗 ...
- python学习-python入门
开始学习python,开始记录. 第一个小程序:登陆系统 功能:1.通过文件名和密码导入用户名和密码~ 2.用户输入用户名和密码 3.将用户输入的用户名进行比对,先判断用户名是否在黑名单里面,如果在黑 ...
- Python学习——Python进程
python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Python提供了非常好用的多进程包multiprocessing,只需要定 ...
- Python学习:python网址收集
Python学习网址收集: 语法学习:http://www.cnblogs.com/hongten/tag/python/ http://www.liaoxuefeng.com ...
- python学习--python 连接SQLServer数据库(两种方法)
1. python 学习.安装教程参照: http://www.runoob.com/python/python-tutorial.html 2. 集成开发环境 JetBrains PyCharm C ...
- Python学习--Python的了解与安装
Python简介: Python 是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python 由荷兰人Guido van Rossum 于 1989 年底发明,第一个公开发行版发行于 19 ...
随机推荐
- Oracle update或alter表被锁住的问题
\\ 查询被锁的对象.来源.sid和serial select object_name, machine, s.sid, s.serial# from v$locked_object l, dba_o ...
- (转)mnist.load_data()出现错误
解决方法:本地导入1.下载mnist.npz文件mnist.npz链接imdb.npz链接2.将上述文件放于合适位置(执行keras程序的python环境中),因为我用的是python虚拟环境,所以我 ...
- kvm中添加VNC密码
#virsh edit 虚机名 <graphics type='vnc' port='5901' autoport='no' listen='0.0.0.0' keymap='en-us'/ ...
- Laravel 在homestead 平台上命令
使用以下命令查看 Heroku 站点地址: $ heroku domains
- [转] python关于ctypes使用char指针与bytes相互转换的问题
最近研究人脸识别,需要用python调用so动态库,涉及到c/c++中的指针字符串转Python的bytes对象的问题. 按照ctypes的文档,直观方式是先创建对应的类型数组,再将指针取地址一一赋值 ...
- Intellij IDEA中使用Debug调试详解
转载:https://www.linuxidc.com/Linux/2017-09/146772.htm Intellij IDEA中使用Debug调试详解 Debug用来追踪代码的运行流程,通常 ...
- leetcode 52 N皇后问题 II
51的简化版,省去根据排列话棋盘的工作,直接计数,代码: class Solution { public: int totalNQueens(int n) { ; vector<); dfs(n ...
- 架构-数据库访问-SQL语言进行连接数据库服务器-OLE:OLE
ylbtech-架构-数据库访问-SQL语言进行连接数据库服务器-OLE:OLE Object Linking and Embedding,对象连接与嵌入,简称OLE技术.OLE 不仅是桌面应用程序集 ...
- json模块:字符串与字典之间的转换--loads,dumps,load,dump
一.json转化成字典: product.json文件:将该文件内容转换成python中字典,方法如下: 方法一:使用.loads(),先读后转换 import json #导入json, 注: ...
- clientdataset 读取excel 如果excel 文件不存在的时候 相应的gird 会不显示数据, 鼠标掠过 gird 格子 才会显示数据。 这是一个bug 哈哈
clientdataset 读取excel 如果excel 文件不存在的时候 相应的gird 会不显示数据, 鼠标掠过 gird 格子 才会显示数据. 这是一个bug 哈哈