yield实现简单的生产者和消费者模型

import time
def consumer(name):
print ('%s 开始消费...' %name)
while True:
commodity=yield
print ('%s被%s消费了...' %(commodity,name))

def producer(name):
c1=consumer('A')
c2=consumer('B')
c1.__next__()
c2.__next__()
print ('%s开始生产了...' %name)
for i in range(10):
time.sleep(1)
c1.send(i)
c2.send(i)
return '结束了...'

producer('zhang')

转载于:https://www.cnblogs.com/zhangtianxia/p/9028085.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用coroutine实现生产者消费者模型可以很好地展示协程的高效性,下面是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/time.h> #include "coroutine.h" #define MAX_SIZE 10 #define PRODUCER_COUNT 3 #define CONSUMER_COUNT 3 // 生产者协程 struct coroutine *producer[MAX_SIZE]; // 消费者协程 struct coroutine *consumer[MAX_SIZE]; // 缓冲区 int buffer[MAX_SIZE]; // 缓冲区的头指针和尾指针 int head = 0; int tail = 0; // 生产者线程函数 void producer_func(void *arg) { int id = *(int *)arg; while (1) { // 判断缓冲区是否已满 while ((tail + 1) % MAX_SIZE == head) { printf("producer %d: buffer is full, yield...\n", id); coroutine_yield(producer[id]); } // 向缓冲区添加数据 int data = rand() % 100; buffer[tail] = data; tail = (tail + 1) % MAX_SIZE; printf("producer %d: produce data %d, buffer size is %d\n", id, data, (tail - head + MAX_SIZE) % MAX_SIZE); // 唤醒一个消费者协程 coroutine_resume(consumer[head]); } } // 消费者线程函数 void consumer_func(void *arg) { int id = *(int *)arg; while (1) { // 判断缓冲区是否为空 while (head == tail) { printf("consumer %d: buffer is empty, yield...\n", id); coroutine_yield(consumer[id]); } // 从缓冲区取出数据 int data = buffer[head]; head = (head + 1) % MAX_SIZE; printf("consumer %d: consume data %d, buffer size is %d\n", id, data, (tail - head + MAX_SIZE) % MAX_SIZE); // 唤醒一个生产者协程 coroutine_resume(producer[id]); } } int main() { // 初始化随机数种子 struct timeval tv; gettimeofday(&tv, NULL); srand(tv.tv_usec); // 创建生产者协程 for (int i = 0; i < PRODUCER_COUNT; i++) { int *id = (int *)malloc(sizeof(int)); *id = i; producer[i] = coroutine_create(producer_func, id); } // 创建消费者协程 for (int i = 0; i < CONSUMER_COUNT; i++) { int *id = (int *)malloc(sizeof(int)); *id = i; consumer[i] = coroutine_create(consumer_func, id); } // 启动生产者协程 for (int i = 0; i < PRODUCER_COUNT; i++) { coroutine_resume(producer[i]); } // 启动消费者协程 for (int i = 0; i < CONSUMER_COUNT; i++) { coroutine_resume(consumer[i]); } // 等待所有协程执行完毕 while (coroutine_pending()) { coroutine_schedule(); } // 销毁协程 for (int i = 0; i < PRODUCER_COUNT; i++) { coroutine_destroy(producer[i]); } for (int i = 0; i < CONSUMER_COUNT; i++) { coroutine_destroy(consumer[i]); } return 0; } ``` 程序中定义了一个大小为10的缓冲区,生产者协程不断向缓冲区添加数据,消费者协程不断从缓冲区取出数据。当缓冲区为空时,消费者协程会yield并等待生产者协程唤醒;当缓冲区已满时,生产者协程会yield并等待消费者协程唤醒。 注意,在生产者消费者协程中,使用coroutine_yield和coroutine_resume函数实现协程的切换,调度器使用coroutine_schedule函数来进行协程的调度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值