{"id":163,"date":"2020-11-23T10:03:01","date_gmt":"2020-11-23T01:03:01","guid":{"rendered":"http:\/\/localhost:8000\/?p=163"},"modified":"2021-01-16T00:30:08","modified_gmt":"2021-01-15T15:30:08","slug":"python-asyncio","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2020\/11\/python-asyncio.html","title":{"rendered":"Python\u3067asyncio\u3092\u4f7f\u3063\u305f\u975e\u540c\u671f\u51e6\u7406"},"content":{"rendered":"
Python\u3067\u975e\u540c\u671f\u51e6\u7406\u3092\u5b9f\u88c5\u3059\u308b\u5834\u5408\u3001 asyncio<\/a> \u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u3046\u306e\u304c\u4e00\u822c\u7684\u3060\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u4e26\u884c\u51e6\u7406\u3092\u3055\u305b\u305f\u308a\u3001\u30cd\u30b9\u30c8\u3057\u305f\u308a\u3059\u308b\u30b1\u30fc\u30b9\u306f\u591a\u5c11\u6c17\u3092\u3064\u3051\u308b\u3053\u3068\u3082\u3042\u3063\u305f\u306e\u3067\u3001\u30e1\u30e2\u7684\u306b\u3084\u308a\u65b9\u3092\u6b8b\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n \u307e\u305a\u306f\u30b7\u30f3\u30d7\u30eb\u306a\u30b1\u30fc\u30b9\u3068\u3057\u3066\u30013\u30b5\u30a4\u30c8\uff08qiita, google, yahoo\uff09\u306e\u5408\u8a086\u30da\u30fc\u30b8\u306b\u3064\u3044\u3066\u3001pyppeteer\uff08chromium\u3092\u8d77\u52d5\u3057\u3066\u975e\u540c\u671f\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\uff09\u3092\u4f7f\u3063\u3066\u3001HTML\u3092\u53d6\u5f97\u3059\u308b\u30b1\u30fc\u30b9\u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n \u5b9f\u884c\u30ed\u30b0\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n \u30ed\u30b0\u304b\u3089\u306f\u4ee5\u4e0b\u306e\u70b9\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n \u4e0a\u8a18\u306e\u4f8b\u3067\u306f\u3001\u30b7\u30fc\u30b1\u30f3\u30b7\u30e3\u30eb\u306b\u975e\u540c\u671f\u51e6\u7406\u3092\u5b9f\u884c\u3057\u3066\u3044\u305f\u305f\u3081\u30c8\u30fc\u30bf\u30eb\u3067\u3051\u3063\u3053\u3046\u5b9f\u884c\u6642\u9593\u304c\u304b\u304b\u3063\u3066\u3057\u307e\u3063\u3066\u3044\u307e\u3057\u305f\u304c\u3001asyncio\u3067\u306f\u540c\u4e00\u306e\u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7\u5185\u306b\u8907\u6570\u306e\u30bf\u30b9\u30af\u304c\u3042\u308b\u5834\u5408\u3001\u3042\u308b\u30bf\u30b9\u30af\u304cawait\u3067\u5f85\u6a5f\u72b6\u614b\u306b\u306a\u3063\u305f\u969b\u306b\u5225\u306e\u30bf\u30b9\u30af\u3092\u5b9f\u884c\u3057\u3066\u304f\u308c\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u975e\u540c\u671f\u51e6\u7406\u3092\u4e26\u884c\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u5b9f\u884c\u30ed\u30b0\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n \u30ed\u30b0\u304b\u3089\u306f\u4ee5\u4e0b\u306e\u70b9\u3092\u78ba\u8a8d\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n Python\u3067\u306f\u3001\u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7\u306e\u4e2d\u3067\u305d\u306e\u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7\u3092\u3082\u3046\u4e00\u56de\u5b9f\u884c\u3057\u305f\u308a\u3001\u5225\u306e\u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7\u3092\u5b9f\u884c\u3057\u305f\u308a\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n RuntimeError: This event loop is already running<\/p>\n RuntimeError: Cannot run the event loop while another loop is running<\/p>\n<\/blockquote>\n \u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7\u3092\u30cd\u30b9\u30c8\u3059\u308b\u5fc5\u7136\u6027\u304c\u3042\u308b\u30b1\u30fc\u30b9\u306f\u7a00\u3060\u3068\u601d\u3046\u306e\u3067\u3059\u304c\u3001\u975e\u540c\u671f\u95a2\u6570 => \u540c\u671f\u95a2\u6570 => \u975e\u540c\u671f\u95a2\u6570\u306e\u3088\u3046\u306a\u3053\u3068\u3092\u3057\u305f\u304f\u306a\u3063\u305f\u6642\u306b\u3053\u306e\u30b1\u30fc\u30b9\u306b\u906d\u9047\u3057\u307e\u3057\u305f\u3002\u56de\u907f\u7b56\u306f\u6bd4\u8f03\u7684\u7c21\u5358\u3067\u3001nest-asyncio<\/a>\u3092\u5c0e\u5165\u3057\u3066\u3042\u3052\u308b\u3060\u3051\u3067\u3059\uff08\u5b9f\u88c5\u4e0a\u306f\u4ee5\u4e0b\u306e2\u884c\uff09\u3002<\/p>\n \u5168\u304f\u5fc5\u7136\u6027\u304c\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7\u3092\u30cd\u30b9\u30c8\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u3092\u4e00\u5fdc\u8f09\u305b\u3066\u304a\u304d\u307e\u3059\u3002 \u4f8b\u3048\u3070\u3001\u901a\u77e5\u3057\u305f\u308a\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u51fa\u529b\u3057\u305f\u308a\u3001\u30dd\u30a4\u30f3\u30c8\u52a0\u7b97\u3057\u305f\u308a\u3001\u975e\u540c\u671f\u3067\u6295\u3052\u3063\u3071\u306a\u3057\u3067\u51e6\u7406\u3092\u5b9f\u884c\u3057\u305f\u3044\u30b1\u30fc\u30b9\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u82f1\u8a9e\u3060\u3068\u6295\u3052\u3063\u3071\u306a\u3057\uff1dfire and forget\u3068\u3044\u3046\u307f\u305f\u3044\u3067\u3059\u3002<\/p>\n AWS\u306eSQS\u3084GCP\u306eGoogle Task Queue\u3092\u4f7f\u3046\u3068\u3044\u3046\u624b\u3082\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u305d\u3093\u306a\u5927\u3052\u3055\u306a\u4ed5\u7d44\u307f\u306b\u3057\u306a\u304f\u3066\u3082\u3001asyncio\u3092\u4f7f\u3063\u3066\u7c21\u5358\u306b\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n run_in_executor<\/a>\u3092\u4f7f\u3063\u3066\u3001\u6307\u5b9a\u3057\u305f\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n \u5b9f\u884c\u30ed\u30b0\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n main()\u95a2\u6570\u306f\u3001\u6295\u3052\u3063\u3071\u306a\u3057\u306e\u51e6\u7406\u304c\u7d42\u308f\u308b\u306e\u3092\u5f85\u305f\u305a\u306b\u5b8c\u4e86\u3057\u3066\u3044\u308b\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n \u307e\u305f\u3001\u30c7\u30b3\u30ec\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u95a2\u6570\u3092\u30e9\u30c3\u30d7\u3059\u308b\u3053\u3068\u3067\u3001\u305d\u306e\u95a2\u6570\u3092\u6295\u3052\u3063\u3071\u306a\u3057\u306b\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002 Python\u3067\u975e\u540c\u671f\u51e6\u7406\u3092\u5b9f\u88c5\u3059\u308b\u5834\u5408\u3001 asyncio \u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u3046\u306e\u304c\u4e00\u822c\u7684\u3060\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u4e26\u884c\u51e6\u7406\u3092\u3055\u305b\u305f\u308a\u3001\u30cd\u30b9\u30c8\u3057\u305f\u308a\u3059\u308b\u30b1\u30fc\u30b9\u306f\u591a\u5c11\u6c17\u3092\u3064\u3051\u308b\u3053\u3068\u3082\u3042\u3063\u305f\u306e\u3067\u3001\u30e1\u30e2\u7684\u306b\u3084\u308a\u65b9\u3092\u6b8b\u3057\u3066\u304a\u304d\u307e\u3059\u3002 \u30b7\u30fc\u30b1\u30f3\u30b7\u30e3\u30eb\u306a\u975e\u540c\u671f\u51e6\u7406 \u307e\u305a\u306f\u30b7\u30f3\u30d7\u30eb\u306a\u30b1\u30fc\u30b9\u3068\u3057\u3066\u30013\u30b5\u30a4\u30c8\uff08qiita, google, yahoo\uff09\u306e\u5408\u8a086\u30da\u30fc\u30b8\u306b\u3064\u3044\u3066\u3001pyppeteer\uff08chromium\u3092\u8d77\u52d5\u3057\u3066\u975e\u540c\u671f\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\uff09\u3092\u4f7f\u3063\u3066\u3001HTML\u3092\u53d6\u5f97\u3059\u308b\u30b1\u30fc\u30b9\u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002 from pyppeteer.browser import Browser from pyppeteer.page import Page from pyppeteer.launcher import launch from typing import List import datetime from asyncio <\/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],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/163"}],"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=163"}],"version-history":[{"count":0,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\u30b7\u30fc\u30b1\u30f3\u30b7\u30e3\u30eb\u306a\u975e\u540c\u671f\u51e6\u7406<\/h2>\n
from pyppeteer.browser import Browser\nfrom pyppeteer.page import Page\nfrom pyppeteer.launcher import launch\nfrom typing import List\nimport datetime\nfrom asyncio import AbstractEventLoop\nimport asyncio\n\nURLS: List[str] = [\n 'https:\/\/qiita.com\/search?q=1',\n 'https:\/\/qiita.com\/search?q=2',\n 'https:\/\/www.google.com\/search?q=1',\n 'https:\/\/www.google.com\/search?q=2',\n 'https:\/\/search.yahoo.co.jp\/search?p=1',\n 'https:\/\/search.yahoo.co.jp\/search?p=2',\n]\n\ndef log(message: str):\n print(f'{datetime.datetime.now()} {message}')\n\nasync def main():\n log('main started.')\n browser: Browser = await launch()\n try:\n htmls: List[str] = []\n for url in URLS:\n html: str = await scrape_one_page(browser, url)\n htmls.append(html)\n finally:\n await browser.close()\n log(f'htmls count: {len(htmls)}')\n log('main finished.')\n\nasync def scrape_one_page(browser: Browser, url: str) -> str:\n """\u3072\u3068\u3064\u306e\u30da\u30fc\u30b8\u3092\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3057\u3066\u3001HTML\u3092\u8fd4\u3059"""\n log(f'scrape_one_page started. url: {url}')\n log(f'task count: {len(asyncio.all_tasks())}')\n page: Page = await browser.newPage()\n try:\n await page.goto(url)\n html: str = await page.content()\n return html\n finally:\n await page.close()\n log(f'scrape_one_page finished. url: {url}')\n\nif __name__ == "__main__":\n event_loop: AbstractEventLoop = asyncio.get_event_loop()\n event_loop.run_until_complete(main())<\/code><\/pre>\n
\n
get_event_loop()<\/code>\u3067\u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7<\/a>\u3092\u53d6\u5f97\u3057\u3066\u3001
run_until_complete()<\/code>\u3067
main()<\/code>\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u5b9f\u884c\u3057\u3066\u3044\u308b<\/li>\n
2020-11-23 11:33:38.684331 main started.\n2020-11-23 11:33:39.001588 scrape_one_page started. url: https:\/\/qiita.com\/search?q=1\n2020-11-23 11:33:39.001651 task count: 4\n2020-11-23 11:33:41.051855 scrape_one_page finished. url: https:\/\/qiita.com\/search?q=1\n2020-11-23 11:33:41.051888 scrape_one_page started. url: https:\/\/qiita.com\/search?q=2\n2020-11-23 11:33:41.051932 task count: 4\n2020-11-23 11:33:42.652274 scrape_one_page finished. url: https:\/\/qiita.com\/search?q=2\n2020-11-23 11:33:42.652317 scrape_one_page started. url: https:\/\/www.google.com\/search?q=1\n2020-11-23 11:33:42.652361 task count: 4\n2020-11-23 11:33:43.849362 scrape_one_page finished. url: https:\/\/www.google.com\/search?q=1\n2020-11-23 11:33:43.849446 scrape_one_page started. url: https:\/\/www.google.com\/search?q=2\n2020-11-23 11:33:43.849474 task count: 4\n2020-11-23 11:33:44.906207 scrape_one_page finished. url: https:\/\/www.google.com\/search?q=2\n2020-11-23 11:33:44.906264 scrape_one_page started. url: https:\/\/search.yahoo.co.jp\/search?p=1\n2020-11-23 11:33:44.906293 task count: 4\n2020-11-23 11:33:46.705226 scrape_one_page finished. url: https:\/\/search.yahoo.co.jp\/search?p=1\n2020-11-23 11:33:46.705264 scrape_one_page started. url: https:\/\/search.yahoo.co.jp\/search?p=2\n2020-11-23 11:33:46.705292 task count: 4\n2020-11-23 11:33:48.536774 scrape_one_page finished. url: https:\/\/search.yahoo.co.jp\/search?p=2\n2020-11-23 11:33:48.554774 htmls count: 6\n2020-11-23 11:33:48.554819 main finished.<\/code><\/pre>\n
\n
\n
main()<\/code>\u3084
Connection._recv_loop()<\/code>\u3084
WebSocketCommonProtocol.transfer_data()<\/code>\u3084
WebSocketCommonProtocol.close_connection()<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
\u4e26\u884c\u3067\u975e\u540c\u671f\u51e6\u7406<\/h2>\n
\n\u3053\u3053\u3067\u306f\u4e0a\u306e\u4f8b\u3092\u3001\u4e26\u884c\u51e6\u7406\u3059\u308b\u5f62\u306bmain()<\/code>\u95a2\u6570\u3092\u5c11\u3057\u66f8\u304d\u63db\u3048\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
async def main():\n log('main started.')\n browser: Browser = await launch()\n try:\n coroutines: List[Coroutine] = []\n for url in URLS:\n coroutines.append(scrape_one_page(browser, url))\n htmls: List[str] = await asyncio.gather(*coroutines)\n finally:\n await browser.close()\n log(f'htmls count: {len(htmls)}')\n log('main finished.')<\/code><\/pre>\n
\n
asyncio.gather()<\/code>\u3067\u4e00\u3064\u306e\u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7\u306b\u307e\u3068\u3081\u3066\u30bf\u30b9\u30af\u3068\u3057\u3066\u767b\u9332\u3057\u3066\u4e26\u884c\u5b9f\u884c\u3059\u308b<\/li>\n
2020-11-23 14:53:33.015661 main started.\n2020-11-23 14:53:33.361009 scrape_one_page started. url: https:\/\/qiita.com\/search?q=1\n2020-11-23 14:53:33.361079 task count: 10\n2020-11-23 14:53:33.361134 scrape_one_page started. url: https:\/\/qiita.com\/search?q=2\n2020-11-23 14:53:33.361158 task count: 11\n2020-11-23 14:53:33.361194 scrape_one_page started. url: https:\/\/www.google.com\/search?q=1\n2020-11-23 14:53:33.361216 task count: 12\n2020-11-23 14:53:33.361249 scrape_one_page started. url: https:\/\/www.google.com\/search?q=2\n2020-11-23 14:53:33.361269 task count: 13\n2020-11-23 14:53:33.361301 scrape_one_page started. url: https:\/\/search.yahoo.co.jp\/search?p=1\n2020-11-23 14:53:33.361333 task count: 14\n2020-11-23 14:53:33.361366 scrape_one_page started. url: https:\/\/search.yahoo.co.jp\/search?p=2\n2020-11-23 14:53:33.361394 task count: 15\n2020-11-23 14:53:34.861235 scrape_one_page finished. url: https:\/\/www.google.com\/search?q=2\n2020-11-23 14:53:35.778646 scrape_one_page finished. url: https:\/\/www.google.com\/search?q=1\n2020-11-23 14:53:35.870635 scrape_one_page finished. url: https:\/\/search.yahoo.co.jp\/search?p=2\n2020-11-23 14:53:36.026315 scrape_one_page finished. url: https:\/\/search.yahoo.co.jp\/search?p=1\n2020-11-23 14:53:36.875245 scrape_one_page finished. url: https:\/\/qiita.com\/search?q=1\n2020-11-23 14:53:36.880065 scrape_one_page finished. url: https:\/\/qiita.com\/search?q=2\n2020-11-23 14:53:36.903932 htmls count: 6\n2020-11-23 14:53:36.903982 main finished.<\/code><\/pre>\n
\n
\n
scrape_one_page<\/code>\u306e\u5b9f\u884c\u3067\u3001
scrape_one_pate()<\/code>\u3068
Connection._async_send()<\/code>\u306e\u4e8c\u3064\u306e\u30bf\u30b9\u30af\u304c\u767b\u9332\u3055\u308c\u3066\u3044\u305f<\/li>\n
\u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7\u3092\u30cd\u30b9\u30c8<\/h2>\n
\n
import nest_asyncio\nnest_asyncio.apply()<\/code><\/pre>\n
\n\u975e\u540c\u671f\u95a2\u6570\uff08main\uff09 => \u540c\u671f\u95a2\u6570\uff08scrape_one_page\uff09 => \u975e\u540c\u671f\u95a2\u6570\uff08scrape_by_pyppeteer\uff09\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u3066\u3001\u6700\u521d\u3068\u6700\u5f8c\u306e\u975e\u540c\u671f\u95a2\u6570\u3092\u5225\u306e\u30a4\u30d9\u30f3\u30c8\u30eb\u30fc\u30d7\u3067\u5b9f\u884c\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\nfrom pyppeteer.browser import Browser\nfrom pyppeteer.page import Page\nfrom pyppeteer.launcher import launch\nfrom typing import List\nimport datetime\nimport requests\nfrom asyncio import AbstractEventLoop\nimport asyncio\nimport nest_asyncio\nnest_asyncio.apply()\n\nURLS: List[str] = [\n 'https:\/\/qiita.com\/search?q=1',\n 'https:\/\/qiita.com\/search?q=2',\n 'https:\/\/www.google.com\/search?q=1',\n 'https:\/\/www.google.com\/search?q=2',\n 'https:\/\/search.yahoo.co.jp\/search?p=1',\n 'https:\/\/search.yahoo.co.jp\/search?p=2',\n]\n\ndef log(message: str):\n print(f'{datetime.datetime.now()} {message}')\n\nasync def main():\n log('main started.')\n htmls: List[str] = []\n for url in URLS:\n htmls.append(scrape_one_page(url))\n log(f'htmls count: {len(htmls)}')\n log('main finished.')\n\ndef scrape_one_page(url: str) -> str:\n """\u3072\u3068\u3064\u306e\u30da\u30fc\u30b8\u3092pyppeteer\u3082\u3057\u304f\u306frequests\u3067\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3057\u3066\u3001HTML\u3092\u8fd4\u3059"""\n if 'qiita' in url:\n # qiita\u306e\u5834\u5408\u306f\u3001pyppeteer\u3092\u4f7f\u3063\u3066\u975e\u540c\u671f\u51e6\u7406\u3067HTML\u3092\u53d6\u5f97\n event_loop: AbstractEventLoop = asyncio.new_event_loop()\n return event_loop.run_until_complete(scrape_by_pyppeteer(url))\n else:\n # \u305d\u308c\u4ee5\u5916\u306f\u3001requests\u3092\u4f7f\u3063\u3066\u540c\u671f\u51e6\u7406\u3067HTML\u3092\u53d6\u5f97\n return requests.get(url).text\n\nasync def scrape_by_pyppeteer(url: str) -> str:\n """pyppeteer\u3067\u30b9\u30af\u30ec\u30a4\u30d4\u30f3\u30b0\u3057\u3066HTML\u3092\u8fd4\u3059"""\n try:\n browser: Browser = await launch()\n page: Page = await browser.newPage()\n await page.goto(url)\n html: str = await page.content()\n return html\n finally:\n await page.close()\n await browser.close()\n\nif __name__ == "__main__":\n event_loop: AbstractEventLoop = asyncio.get_event_loop()\n event_loop.run_until_complete(main())<\/code><\/pre>\n
\u6295\u3052\u3063\u3071\u306a\u3057\uff08fire and forget\uff09<\/h2>\n
import datetime\nimport asyncio\nimport time\n\ndef main():\n log('main started.')\n asyncio.get_event_loop().run_in_executor(None, wait, 10)\n log('main finished.')\n\ndef wait(second: int):\n """\u6295\u3052\u3063\u3071\u306a\u3057\u306b\u3057\u305f\u3044\u51e6\u7406"""\n log('wait started.')\n time.sleep(second)\n log('wait finished.')\n\ndef log(message: str):\n print(f'{datetime.datetime.now()} {message}')\n\nif __name__ == "__main__":\n main()<\/code><\/pre>\n
\n
2020-11-28 20:02:19.608202 main started.\n2020-11-28 20:02:19.611073 wait started.\n2020-11-28 20:02:19.611128 main finished.\n2020-11-28 20:02:29.611322 wait finished.<\/code><\/pre>\n
\n\u3053\u3061\u3089\u306e\u65b9\u304c\u5b9f\u88c5\u304c\u30b9\u30c3\u30ad\u30ea\u3059\u308b\u306e\u3067\u3001\u500b\u4eba\u7684\u306b\u306f\u3053\u3061\u3089\u3092\u5229\u7528\u3057\u3066\u307e\u3059\u3002<\/p>\nimport datetime\nimport asyncio\nimport time\n\ndef fire_and_forget(f):\n """\u5bfe\u8c61\u95a2\u6570\u3092\u975e\u540c\u671f\u3067\u6295\u3052\u3063\u3071\u306a\u3057\u306b\u3059\u308b\u305f\u3081\u306e\u30c7\u30b3\u30ec\u30fc\u30bf"""\n def wrapped(*args, **kwargs):\n return asyncio.get_event_loop().run_in_executor(None, f, *args, *kwargs)\n return wrapped\n\ndef main():\n log('main started.')\n wait(10) # \u901a\u5e38\u901a\u308a\u547c\u3073\u51fa\u305b\u3070\u3001\u30c7\u30b3\u30ec\u30fc\u30bf\u304c\u6295\u3052\u3063\u3071\u306a\u3057\u306b\u3057\u3066\u304f\u308c\u308b\n log('main finished.')\n\n@fire_and_forget\ndef wait(second: int):\n """\u6295\u3052\u3063\u3071\u306a\u3057\u306b\u3057\u305f\u3044\u51e6\u7406"""\n log('wait started.')\n time.sleep(second)\n log('wait finished.')\n\ndef log(message: str):\n print(f'{datetime.datetime.now()} {message}')\n\nif __name__ == "__main__":\n main()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"