{"id":202,"date":"2020-09-20T01:05:57","date_gmt":"2020-09-19T16:05:57","guid":{"rendered":"http:\/\/localhost:8000\/?p=202"},"modified":"2021-01-16T01:07:56","modified_gmt":"2021-01-15T16:07:56","slug":"python-generator-asyncgenerator","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2020\/09\/python-generator-asyncgenerator.html","title":{"rendered":"python\u306eGenerator\u3068AsyncGenerator\u306e\u4f7f\u3044\u65b9"},"content":{"rendered":"
python\u306e generator<\/a> \u306b\u3064\u3044\u3066\u3001yield\u306f\u3082\u3068\u3082\u3068\u305f\u307e\u306b\u4f7f\u3063\u3066\u305f\u306e\u3067\u3059\u304c<\/p>\n \u3092\u77e5\u3063\u305f\u306e\u3067\u3001\u8efd\u304f\u8a18\u4e8b\u3092\u66f8\u3044\u3066\u304a\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306f\u3001 List\u3092\u8fd4\u3057\u3066\u51e6\u7406\u3067\u304d\u308b\u30b1\u30fc\u30b9\u3067\u3042\u308c\u3070\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306b\u3059\u308b\u5fc5\u8981\u306f\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001<\/p>\n \u306a\u3069\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u3092\u5229\u7528\u3057\u305f\u65b9\u304c\u826f\u3044\u30b1\u30fc\u30b9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n \u4e00\u756a\u30b7\u30f3\u30d7\u30eb\u306ayield\u3060\u3051\u3092\u884c\u3046\u30b5\u30f3\u30d7\u30eb\u3067\u3059\u3002<\/p>\n \u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u30ed\u30b0\u304c\u51fa\u529b\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n \u30dd\u30a4\u30f3\u30c8\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\n \u6700\u8fd1\u77e5\u3063\u305f\u306e\u3067\u3059\u304c\u3001yield\u3057\u3066\u4e00\u6642\u505c\u6b62\u3057\u305f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306b\u3001\u547c\u3073\u51fa\u3057\u5143\u304b\u3089 \u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u30ed\u30b0\u304c\u51fa\u529b\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n \u30dd\u30a4\u30f3\u30c8\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\n \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u3082\u3001 \u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u30ed\u30b0\u304c\u51fa\u529b\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n \u30dd\u30a4\u30f3\u30c8\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\n \u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u51e6\u7406\u306a\u3069\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u5185\u3067\u975e\u540c\u671f\u51e6\u7406\u3092\u4f7f\u3044\u305f\u3044\u30b1\u30fc\u30b9\u3082\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u8abf\u3079\u305f\u3068\u3053\u308dAsyncGenerator\u3068\u3044\u3046\u3084\u3064\u304c\u3044\u3066\u3001\u901a\u5e38\u306eGenerator\u3068\u307b\u307c\u540c\u3058\u4f7f\u3044\u65b9\u3067\u975e\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u3092\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002 \u5358\u7d14\u306b \u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u30ed\u30b0\u304c\u51fa\u529b\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n \u30dd\u30a4\u30f3\u30c8\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\n \u975e\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3067\u3082\u547c\u3073\u51fa\u3057\u5143\u304b\u3089\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306b\u5024\u3092\u901a\u77e5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n \u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u30ed\u30b0\u304c\u51fa\u529b\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n \u30dd\u30a4\u30f3\u30c8\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\n \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306f\u3001 \u3068\u304f\u306b\u30d8\u30c3\u30c9\u30ec\u30b9\u30d6\u30e9\u30a6\u30b6\u3092\u4f7f\u3063\u305f\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u307f\u305f\u3044\u306a\u3001\u30ea\u30b9\u30c8\u3068\u3057\u3066\u4f55\u304b\u3092\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u3088\u3046\u306a\u51e6\u7406\u3068\u306f\u76f8\u6027\u304c\u3088\u304b\u3063\u305f\u3067\u3059\u3002<\/p>\n \u304b\u306a\u308a\u4fbf\u5229\u306a\u6a5f\u80fd\u306a\u306e\u3067\u4eca\u5f8c\u3082\u4f7f\u3044\u5012\u3057\u3066\u3044\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" python\u306e generator \u306b\u3064\u3044\u3066\u3001yield\u306f\u3082\u3068\u3082\u3068\u305f\u307e\u306b\u4f7f\u3063\u3066\u305f\u306e\u3067\u3059\u304c send\u3084return\u3082\u3067\u304d\u308b\u3053\u3068 \u975e\u540c\u671f\u51e6\u7406\u3067\u3082\u4f7f\u3048\u308b\u3053\u3068 \u3092\u77e5\u3063\u305f\u306e\u3067\u3001\u8efd\u304f\u8a18\u4e8b\u3092\u66f8\u3044\u3066\u304a\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002 \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\uff08Generator\uff09 \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306f\u3001\u5024\u3092\u8fd4\u3059\u4ee3\u308f\u308a\u306b\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\uff08\u4e00\u9023\u306e\u5024\u3092\u8fd4\u3059\u7279\u6b8a\u306a\u30a4\u30c6\u30ec\u30fc\u30bf\uff09\u3092\u8fd4\u3059\u95a2\u6570\u3067\u3001yield\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u542b\u3080\u95a2\u6570\u306f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u3067\u3059\u3002 \u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306e\u4e2d\u3067yield\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u3068\u3001\u4e00\u65e6\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306f\u4e00\u6642\u505c\u6b62\u3057\u3066\u547c\u3073\u51fa\u3057\u306e\u51e6\u7406\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002\u6b21\u306b\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306e__next__()\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\uff08for\u6587\u3084next(gen)\u306a\u3069\uff09\u3068\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306e\u51e6\u7406\u304c\u518d\u958b\u3055\u308c\u307e\u3059\u3002 List\u3092\u8fd4\u3057\u3066\u51e6\u7406\u3067\u304d\u308b\u30b1\u30fc\u30b9\u3067\u3042\u308c\u3070\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306b\u3059\u308b\u5fc5\u8981\u306f\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001 \u5024\u3092yield\uff08\u751f\u6210\uff09\u3057\u305f\u6642\u306e\u72b6\u614b\u3067\u4f55\u304b\u51e6\u7406\u3092\u3057\u305f\u3044\u5834\u5408\uff08List\u306b\u3059\u308b\u3068 <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,9],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/202"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":0,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\n
\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\uff08Generator\uff09<\/h2>\n
\u5024<\/code>\u3092\u8fd4\u3059\u4ee3\u308f\u308a\u306b
\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\uff08\u4e00\u9023\u306e\u5024\u3092\u8fd4\u3059\u7279\u6b8a\u306a\u30a4\u30c6\u30ec\u30fc\u30bf\uff09<\/code>\u3092\u8fd4\u3059\u95a2\u6570\u3067\u3001
yield<\/code>\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u542b\u3080\u95a2\u6570\u306f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u3067\u3059\u3002
\n\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306e\u4e2d\u3067yield<\/code>\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u3068\u3001\u4e00\u65e6\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306f\u4e00\u6642\u505c\u6b62\u3057\u3066\u547c\u3073\u51fa\u3057\u306e\u51e6\u7406\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002\u6b21\u306b\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306e
__next__()<\/code>\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\uff08
for<\/code>\u6587\u3084
next(gen)<\/code>\u306a\u3069\uff09\u3068\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306e\u51e6\u7406\u304c\u518d\u958b\u3055\u308c\u307e\u3059\u3002<\/p>\n
\n
yield<\/h3>\n
import time\nfrom typing import Generator\n\ndef main():\n for i in generate():\n print(f'get. i: {i}')\n time.sleep(1)\n\ndef generate() -> Generator[int, None, None]:\n print('generate started.')\n for i in range(3):\n print(f'before yield. i: {i}')\n yield i\n print(f'after yield. i: {i}')\n print('generate finished.')\n\nif __name__ == "__main__":\n main()<\/code><\/pre>\n
generate started.\nbefore yield. i: 0\nget. i: 0\nafter yield. i: 0\nbefore yield. i: 1\nget. i: 1\nafter yield. i: 1\nbefore yield. i: 2\nget. i: 2\nafter yield. i: 2\ngenerate finished.<\/code><\/pre>\n
\n
before yield<\/code> =>
get<\/code> =>
after yield<\/code> \u306e\u9806\u3067\u30ed\u30b0\u304c\u51fa\u529b\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u304b\u3089\u3001yield\u3057\u305f\u6642\u70b9\u3067\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306f\u4e00\u6642\u505c\u6b62\u3057\u3001\u547c\u3073\u51fa\u3057\u5143\u306b\u51e6\u7406\u304c\u3046\u3064\u3063\u3066\u3044\u308b\n
\n
get<\/code>\u306e\u5f8c1\u79d2sleep\u3057\u3066\u3044\u308b\u304c\u305d\u306e\u9593\u3082\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306f\u505c\u6b62\u3057\u305f\u307e\u307e<\/li>\n
generate started<\/code>\u3068
generate finished<\/code>\u306f\u305d\u308c\u305e\u308c1\u56de\u3057\u304b\u51fa\u529b\u3055\u308c\u3066\u306a\u3044\u3053\u3068\u304b\u3089\u3001
generate()<\/code>\u306f\u4e00\u5ea6\u3057\u304b\u547c\u3073\u51fa\u3055\u308c\u306a\u3044<\/li>\n<\/ul>\n
send<\/h3>\n
generator.send()<\/code>\u3092\u4f7f\u3063\u3066\u5024\u3092\u6e21\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\n\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u304b\u3089\u53d7\u3051\u53d6\u3063\u305f\u5f8c\u547c\u3073\u51fa\u3057\u5143\u3067\u51e6\u7406\u3057\u305f\u7d50\u679c\u3067\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306e\u6319\u52d5\u3092\u5909\u3048\u305f\u3044\u6642\u306b\u4f7f\u3046\u3082\u306e\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\nimport time\nfrom typing import Generator, Optional\nfrom random import randint\n\ndef main():\n gen: Generator = generate() # Generator\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u751f\u6210\n i: int = 0\n while True:\n try:\n print(f'before next. i: {i}')\n i = next(gen)\n print(f'after next. i: {i}')\n time.sleep(1)\n send_value: int = randint(1, 10) # 1\u301c10\u306e\u30e9\u30f3\u30c0\u30e0\u306a\u6574\u6570\u3092\u53d6\u5f97\n print(f'before send. i: {i}, send_value: {send_value}')\n gen.send(send_value)\n print(f'after send. i: {i}, send_value: {send_value}')\n\n except StopIteration:\n print('StopIteration raised.')\n break\n\ndef generate() -> Generator[int, int, None]:\n print('generate started')\n i: int = 1\n while True:\n print(f'before yield. i: {i}')\n received_value: Optional[str] = (yield i) # next()\u3067\u3082send()\u3067\u3082\u3053\u3053\u306b\u6765\u308b\u3002next()\u3060\u3068received_value\u306fNone\n print(f'after yield. i: {i}, received_value: {received_value}')\n if received_value is not None:\n i = i + received_value # None\u3058\u3083\u306a\u3051\u308c\u3070i\u306b\u53d7\u3051\u53d6\u3063\u305f\u5024\u3092\u52a0\u7b97\n if i > 10: # 10\u3092\u8d85\u3048\u305f\u3089loop\u7d42\u4e86\n break\n print('generate finished')\n\nif __name__ == "__main__":\n main()\n<\/code><\/pre>\n
before next. i: 0\ngenerate started\nbefore yield. i: 1\nafter next. i: 1\nbefore send. i: 1, send_value: 6\nafter yield. i: 1, received_value: 6\nbefore yield. i: 7\nafter send. i: 1, send_value: 6\nbefore next. i: 1\nafter yield. i: 7, received_value: None\nbefore yield. i: 7\nafter next. i: 7\nbefore send. i: 7, send_value: 8\nafter yield. i: 7, received_value: 8\ngenerate finished\nStopIteration raised.<\/code><\/pre>\n
\n
send()<\/code>\u306f\u4f7f\u3048\u306a\u3044\u306e\u3067\u3001Generator\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b<\/li>\n
before next<\/code>\u306e\u5f8c\u306b
gererate started<\/code>\u304c\u51fa\u529b\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001Generator\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u3066\u3082\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u5185\u90e8\u306e\u51e6\u7406\u306f\u52d5\u3044\u3066\u306a\u3044<\/li>\n
next(generator)<\/code>\u3067\u3082
generator.send()<\/code>\u3067\u3082\u51e6\u7406\u304c\u518d\u958b\u3055\u308c\u308b<\/li>\n
send()<\/code>\u306e\u5834\u5408\u306f\u5024\u3092\u53d7\u3051\u53d6\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u304c\u3001
next()<\/code>\u306e\u5834\u5408\u306f
None<\/code>\u3092\u53d7\u3051\u53d6\u308b<\/li>\n
StopIteration<\/code>\u304craise\u3055\u308c\u308b<\/li>\n<\/ul>\n
return<\/h3>\n
return<\/code>\u3059\u308b\u3053\u3068\u3067\u95a2\u6570\u81ea\u4f53\u306e\u623b\u308a\u5024\u3092\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306f\u3082\u3068\u3082\u3068Generator\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8fd4\u5374\u3059\u308b\u305f\u3081
return value<\/code>\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001
StopIteration(value)<\/code>\u3092raise\u3059\u308b\u3068\u3044\u3046\u5c11\u3057\u7279\u6b8a\u306a\u52d5\u304d\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
import time\nfrom typing import Generator\n\ndef main():\n gen: Generator[int, None, str] = generate()\n i: int = 0\n while True:\n try:\n print(f'before next. i: {i}')\n i: int = next(gen)\n print(f'after next. i: {i}')\n time.sleep(1)\n except StopIteration as e:\n print(f'StopIteration raised. return_value: {e.value}')\n break\n\ndef generate() -> Generator[int, None, str]:\n print('generate started.')\n for i in range(3):\n yield i\n print('generate finished.')\n return '##return_value##'\n\nif __name__ == "__main__":\n main()<\/code><\/pre>\n
before next. i: 0\ngenerate started.\nafter next. i: 0\nbefore next. i: 0\nafter next. i: 1\nbefore next. i: 1\nafter next. i: 2\nbefore next. i: 2\ngenerate finished.\nStopIteration raised. return_value: ##return_value##<\/code><\/pre>\n
\n
except<\/code>\u3067\u30ad\u30e3\u30c3\u30c1\u3057\u3066\u53d7\u3051\u53d6\u308b\u5fc5\u8981\u304c\u3042\u308b<\/li>\n<\/ul>\n
\u975e\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\uff08AsyncGenerator\uff09<\/h2>\n
\n\u306a\u304a\u3001\u901a\u5e38\u306e\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3068\u9055\u3063\u3066\u975e\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u306f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u81ea\u4f53\u306e\u623b\u308a\u5024\u3092\u8fd4\u3059\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\nyield<\/h3>\n
yield<\/code>\u3059\u308b\u3060\u3051\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u3059\u3002\u4ee5\u4e0b\u3092\u78ba\u8a8d\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
import asyncio\nfrom typing import AsyncGenerator\n\nasync def main():\n print('====== async for\u306e\u4f8b ======')\n async for i in async_generate():\n print(f'started loop. i: {i}')\n await asyncio.sleep(1)\n\n print('====== generator\u3092\u53d6\u5f97\u3057\u3066while\u3067\u56de\u3059\u4f8b ======')\n gen: AsyncGenerator[int, None] = async_generate()\n i: int = 0\n while True:\n try:\n print(f'before anext. i: {i}')\n i = await gen.__anext__()\n print(f'after anext. i: {i}')\n except StopAsyncIteration:\n break\n\nasync def async_generate() -> AsyncGenerator[int, None]:\n print('async_generate started.')\n for i in range(3):\n print(f'before yield. i: {i}')\n yield i\n print(f'after yield. i: {i}')\n await asyncio.sleep(1)\n print('async_generate finished.')\n\nif __name__ == "__main__":\n asyncio.get_event_loop().run_until_complete(\n main()\n )<\/code><\/pre>\n
====== async for\u306e\u4f8b ======\nasync_generate started.\nbefore yield. i: 0\nstarted loop. i: 0\nafter yield. i: 0\nbefore yield. i: 1\nstarted loop. i: 1\nafter yield. i: 1\nbefore yield. i: 2\nstarted loop. i: 2\nafter yield. i: 2\nasync_generate finished.\n====== generator\u3092\u53d6\u5f97\u3057\u3066while\u3067\u56de\u3059\u4f8b ======\nbefore anext. i: 0\nasync_generate started.\nbefore yield. i: 0\nafter anext. i: 0\nbefore anext. i: 0\nafter yield. i: 0\nbefore yield. i: 1\nafter anext. i: 1\nbefore anext. i: 1\nafter yield. i: 1\nbefore yield. i: 2\nafter anext. i: 2\nbefore anext. i: 2\nafter yield. i: 2\nasync_generate finished.<\/code><\/pre>\n
\n
async def<\/code>\u3067\u95a2\u6570\u5b9a\u7fa9\u3059\u308b\u3060\u3051\u3067\u3001\u975e\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u3092\u4f5c\u6210\u3067\u304d\u308b<\/li>\n
async for<\/code>\u3067\u975e\u540c\u671f\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u30a4\u30c6\u30ec\u30fc\u30c8\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b<\/li>\n
await AsyncGenerator.__anext__()<\/code>\u3067\u30a4\u30c6\u30ec\u30fc\u30c8\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u308b\n
\n
anext(AsyncGenerator)<\/code>\u3084
next(AsyncGenerator)<\/code>\u306f\u5b58\u5728\u3057\u306a\u3044<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
asend<\/h3>\n
import asyncio\nfrom typing import AsyncGenerator\nfrom random import randint\n\nasync def main():\n gen: AsyncGenerator[int, None] = async_generate()\n i: int = 0\n while True:\n try:\n print(f'before anext. i: {i}')\n i = await gen.__anext__()\n print(f'after anext. i: {i}')\n send_value: int = randint(1, 10)\n print(f'before anext. i: {i}, send_value: {send_value}')\n await gen.asend(send_value)\n print(f'after anext. i: {i}, send_value: {send_value}')\n except StopAsyncIteration:\n break\n\nasync def async_generate() -> AsyncGenerator[int, None]:\n print('async_generate started.')\n for i in range(10):\n print(f'before yield. i: {i}')\n received_value: int = (yield i)\n print(f'after yield. i: {i}, received_value: {received_value}')\n if received_value is not None and received_value > 5:\n print('finish async_generate because received_value > 0.')\n break # \u53d7\u3051\u53d6\u3063\u305f\u5024\u304c5\u3088\u308a\u5927\u304d\u304b\u3063\u305f\u3089\u7d42\u4e86\u3059\u308b\n await asyncio.sleep(1)\n print('async_generate finished.')\n\nif __name__ == "__main__":\n asyncio.get_event_loop().run_until_complete(\n main()\n )<\/code><\/pre>\n
before anext. i: 0\nasync_generate started.\nbefore yield. i: 0\nafter anext. i: 0\nbefore anext. i: 0, send_value: 7\nafter yield. i: 0, received_value: 7\nfinish async_generate because received_value > 0.\nasync_generate finished.<\/code><\/pre>\n
\n
await AsyncGenerator.asend()<\/code>\u3067\u901a\u77e5\u3059\u308b\uff08await\u3092\u5fd8\u308c\u305a\u306b\uff09<\/li>\n
send()<\/code>\u3092\u4f7f\u3046\u30b1\u30fc\u30b9\u3068\u540c\u3058<\/li>\n<\/ul>\n
\u3055\u3044\u3054\u306b<\/h2>\n
yield<\/code>\u3057\u305f\u6642\u306b\u4e00\u65e6\u51e6\u7406\u3092\u505c\u6b62\u3057\u3066\u547c\u3073\u51fa\u3057\u5143\u306e\u51e6\u7406\u304c\u5b8c\u4e86\u3057\u305f\u3089\u6b21\u306e\u51e6\u7406\u3092\u884c\u3048\u308b\u3068\u3053\u308d\u304c\u3068\u3066\u3082\u826f\u304f\u3066\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u899a\u3048\u3066\u304b\u3089\u3001for\u6587\u304c\u6df1\u304f\u306a\u3063\u3066\u3044\u3084\u3060\u306a\u30fc\u3068\u601d\u3063\u3066\u3044\u305f\u7b87\u6240\u3092\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u306b\u3057\u3066\u968e\u5c64\u3092\u5c11\u306a\u304f\u3057\u3066\u53ef\u8aad\u6027\u304c\u3060\u3044\u3076\u4e0a\u304c\u3063\u305f\u3088\u3046\u306b\u611f\u3058\u307e\u3059\uff08\u591a\u5c11\u8aad\u307f\u624b\u306e\u30ea\u30c6\u30e9\u30b7\u30fc\u304c\u6c42\u3081\u3089\u308c\u308b\u90e8\u5206\u306f\u3042\u308a\u307e\u3059\u304c\uff09\u3002<\/p>\n