coroutine.py 459 B

123456789101112131415161718192021222324
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. def consumer():
  4. r = ''
  5. while True:
  6. n = yield r
  7. if not n:
  8. return
  9. print('[CONSUMER] Consuming %s...' % n)
  10. r = '200 OK'
  11. def produce(c):
  12. c.send(None)
  13. n = 0
  14. while n < 5:
  15. n = n + 1
  16. print('[PRODUCER] Producing %s...' % n)
  17. r = c.send(n)
  18. print('[PRODUCER] Consumer return: %s' % r)
  19. c.close()
  20. c = consumer()
  21. produce(c)