前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 多线程 信号量semaphore(并行编程 5)

python 多线程 信号量semaphore(并行编程 5)

作者头像
用户5760343
发布2019-07-30 11:02:11
6900
发布2019-07-30 11:02:11
举报
文章被收录于专栏:sktjsktjsktj

import threading import time import random

semaphore = threading.Semaphore(0)

def consumer(): print("consumer is waiting.") semaphore.acquire() print("Consumer notify: consumed item number %s." % item)

def producer(): global item time.sleep(10) item = random.randint(1, 1000) print("producer nofity: produced item number %s." % item) semaphore.release()

if name == "main":

for i in range(0, 5):
    t1 = threading.Thread(target=producer)
    t2 = threading.Thread(target=consumer)
    t1.start()
    t2.start()
    t1.join()
    t2.join()
print("program teminated.")

threading.Semaphore(0)

se.acquire() se.release()

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.07.27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • threading.Semaphore(0)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档