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. 1 python学习——python环境配置

    1 python学习--python环境配置 要学习python语言,光看书看教程还是不好,得动手去写.当然,不管学习什么编程语言,最佳的方式还在于实践. 要实践,先得有一个Python解释器来解释执 ...

  2. Python学习---Python安装与基础1205

    1.0. 安装 1.1.1. 下载 官网下载地址:https://www.python.org/downloads/release/python-352/ 1.1.2. 配置环境变量 因为在安装的时候 ...

  3. Python学习---Python下[元组]的学习

    元组是不可变的, 用小括号()定义,而且一旦定义 ,不可变[类型是tuple] [元组看做一个整体,不可拆分,不可赋值,但可以全部重新赋值] 通过圆括号,用逗号分隔,常用在使语句或用户定义的函数能够安 ...

  4. Python学习---Python的异步IO[all]

    1.1.1. 前期环境准备和基础知识 安装: pip3 install aiohttp pip3 install grequests pip3 install wheel pip3 install s ...

  5. Python学习--Python基础语法

    第一个Python程序 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码. linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗 ...

  6. python学习-python入门

    开始学习python,开始记录. 第一个小程序:登陆系统 功能:1.通过文件名和密码导入用户名和密码~ 2.用户输入用户名和密码 3.将用户输入的用户名进行比对,先判断用户名是否在黑名单里面,如果在黑 ...

  7. Python学习——Python进程

    python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Python提供了非常好用的多进程包multiprocessing,只需要定 ...

  8. Python学习:python网址收集

    Python学习网址收集: 语法学习:http://www.cnblogs.com/hongten/tag/python/             http://www.liaoxuefeng.com ...

  9. python学习--python 连接SQLServer数据库(两种方法)

    1. python 学习.安装教程参照: http://www.runoob.com/python/python-tutorial.html 2. 集成开发环境 JetBrains PyCharm C ...

  10. Python学习--Python的了解与安装

    Python简介: Python 是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python 由荷兰人Guido van Rossum 于 1989 年底发明,第一个公开发行版发行于 19 ...

随机推荐

  1. 对象转json串.

    public static Object returnObject(String jsonStr, Class objClass) {if (jsonStr == null) {return null ...

  2. wowza 降低延迟

    转自:http://www.ttstream.com/wowza/live/howToAchieveTheLowestLatencyFromCaptureToPlayback   这篇文章介绍了在用R ...

  3. MySQL删除外键约束问题

    当我们在一个表中添加字段约束的时候: ALTER TABLE product ADD CONSTRAINT product_fk FOREIGN KEY(category_id) REFERENCES ...

  4. 1px渲染成2px的场景及解决方案

    1.场景一: IE6 下默认div最小高度为2px,如何创建高为1px的容器? .minContainer{font-size:0px;overflow:hidden}   2.场景二: 移动端高分辨 ...

  5. 【转】mackbook wifi卡死未响应的问题

    原文:http://tieba.baidu.com/p/6140144143?traceid= 1. wifi未响应了,紧急处理法:打开活动监视器,搜索airportd,结束掉进程 2. 彻底解决办法 ...

  6. iOS 命令行打包--xcworkspace

    参考: 打包的具体操作步骤: https://www.jianshu.com/p/6a0aa8cd2e97 打包时使用到的参数详解,参考这篇: https://debugtalk.com/post/i ...

  7. mysql5.7.23性能调优之innodb_buffer_pool_size

    前言 我的数据库版本是5.7.23,最近发现执行SQL越来越慢,一条SQL语句执行需要将近30s. 对于原因,查询资料, https://www.cnblogs.com/qwangxiao/p/892 ...

  8. Java ——正则表达式

    本节重点思维导图 详细见 ————>  正则表达式 [各种语法和方法]

  9. Java容器框架总结(一)

    本篇根据<Java编程思想> 第11章 持有对象 整理,总结Java容器框架中常用集合类及接口的特点及使用场景. (一)总结 1)数组将数字与对象联系起来:可以保存基本类型的数据:一旦生成 ...

  10. MyBatis基础面试题

    转自:http://www.cnblogs.com/huajiezh/p/6415322.html 1.Mybatis基础: #{...} 和 ${...} 的区别MyBatis将 #{…} 解释为J ...