{"id":98726,"date":"2024-03-21T23:57:50","date_gmt":"2024-03-21T14:57:50","guid":{"rendered":"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/"},"modified":"2024-03-28T02:28:28","modified_gmt":"2024-03-27T17:28:28","slug":"python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/","title":{"rendered":"python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528"},"content":{"rendered":"<p>Python\u306emultiprocessing\u30e2\u30b8\u30e5\u30fc\u30eb\u306eLock\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u30de\u30eb\u30c1\u30d7\u30ed\u30bb\u30b9\u30ed\u30c3\u30af\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u591a\u30d7\u30ed\u30bb\u30b9\u306b\u304a\u3051\u308b\u30ed\u30c3\u30af\u306e\u7c21\u5358\u306a\u4f7f\u7528\u4f8b\u3092\u6b21\u306b\u793a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">import<\/span> multiprocessing\r\n\r\n<span class=\"hljs-comment\"># \u5171\u4eab\u53d8\u91cf<\/span>\r\ncounter = multiprocessing.Value(<span class=\"hljs-string\">'i'<\/span>, <span class=\"hljs-number\">0<\/span>)\r\nlock = multiprocessing.Lock()\r\n\r\n<span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title function_\">increment<\/span>():\r\n    <span class=\"hljs-keyword\">for<\/span> _ <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-built_in\">range<\/span>(<span class=\"hljs-number\">100000<\/span>):\r\n        <span class=\"hljs-keyword\">with<\/span> lock:\r\n            counter.value += <span class=\"hljs-number\">1<\/span>\r\n\r\n<span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title function_\">main<\/span>():\r\n    processes = []\r\n    <span class=\"hljs-keyword\">for<\/span> _ <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-built_in\">range<\/span>(<span class=\"hljs-number\">3<\/span>):\r\n        process = multiprocessing.Process(target=increment)\r\n        process.start()\r\n        processes.append(process)\r\n\r\n    <span class=\"hljs-keyword\">for<\/span> process <span class=\"hljs-keyword\">in<\/span> processes:\r\n        process.join()\r\n\r\n    <span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">f\"Counter value: <span class=\"hljs-subst\">{counter.value}<\/span>\"<\/span>)\r\n\r\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\r\n    main()\r\n<\/code><\/pre>\n<p>\u4e0a\u8a18\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u3001\u30de\u30eb\u30c1\u30d7\u30ed\u30bb\u30b9\u3067\u5171\u6709\u3067\u304d\u308b\u6574\u6570\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u306b\u3001multiprocessing.Value\u3092\u4f7f\u7528\u3057\u305fcounter\u5171\u6709\u5909\u6570\u3092\u5b9a\u7fa9\u3057\u307e\u3057\u305f\u3002\u305d\u3057\u3066\u3001\u30d7\u30ed\u30bb\u30b9\u9593\u3067\u540c\u671f\u3092\u53d6\u308b\u305f\u3081\u306b\u3001lock\u30ed\u30c3\u30af\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u5404\u30d7\u30ed\u30bb\u30b9\u3067\u5b9f\u884c\u3059\u308b\u30ed\u30b8\u30c3\u30af\u306f\u3001increment\u95a2\u6570\u304cwith lock\u6587\u3067\u30ed\u30c3\u30af\u3092\u53d6\u5f97\u3057\u3001counter.value\u306b\u5bfe\u3057\u3066\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u64cd\u4f5c\u3092\u884c\u3046\u3082\u306e\u3067\u3059\u3002<\/p>\n<p>main\u95a2\u6570\u3067\u30013\u3064\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u4f5c\u6210\u3057\u3066\u3001processes\u30ea\u30b9\u30c8\u306b\u8ffd\u52a0\u3057\u307e\u3057\u305f\u3002\u305d\u306e\u3042\u3068\u3001process.join()\u306b\u3088\u3063\u3066\u3001\u3059\u3079\u3066\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u5b9f\u884c\u3055\u308c\u308b\u306e\u3092\u5f85\u6a5f\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u6700\u5f8c\u306b\u3001counter.value \u306e\u5024\u3092\u51fa\u529b\u3057\u3066\u3001\u8907\u6570\u30d7\u30ed\u30bb\u30b9\u306b\u3088\u308b\u5171\u6709\u5909\u6570\u306e\u5909\u66f4\u304c\u6b63\u3057\u304f\u884c\u308f\u308c\u305f\u3053\u3068\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<p>\u30ed\u30c3\u30af\u306e\u4f7f\u7528\u306f\u3001\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u304c\u767a\u751f\u3059\u308b\u305f\u3081\u3001\u53ef\u80fd\u306a\u9650\u308a\u6700\u5c0f\u9650\u306b\u6291\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u30ed\u30c3\u30af\u306f\u3001\u5171\u6709\u30ea\u30bd\u30fc\u30b9\u306b\u6392\u4ed6\u30a2\u30af\u30bb\u30b9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306b\u306e\u307f\u4f7f\u7528\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u306emultiprocessing\u30e2\u30b8\u30e5\u30fc\u30eb\u306eLock\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u30de\u30eb\u30c1\u30d7\u30ed\u30bb\u30b9\u30ed\u30c3\u30af\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002 \u591a\u30d7\u30ed\u30bb\u30b9\u306b\u304a\u3051\u308b\u30ed\u30c3\u30af\u306e\u7c21\u5358\u306a\u4f7f\u7528\u4f8b\u3092\u6b21\u306b\u793a\u3057\u307e\u3059\u3002 import multiprocessing  [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-98726","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528 - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/ja\/blog\/python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528\/\" \/>\n<meta property=\"og:locale\" content=\"ja_JP\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528\" \/>\n<meta property=\"og:description\" content=\"Python\u306emultiprocessing\u30e2\u30b8\u30e5\u30fc\u30eb\u306eLock\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u30de\u30eb\u30c1\u30d7\u30ed\u30bb\u30b9\u30ed\u30c3\u30af\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002 \u591a\u30d7\u30ed\u30bb\u30b9\u306b\u304a\u3051\u308b\u30ed\u30c3\u30af\u306e\u7c21\u5358\u306a\u4f7f\u7528\u4f8b\u3092\u6b21\u306b\u793a\u3057\u307e\u3059\u3002 import multiprocessing [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/ja\/blog\/python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-21T14:57:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-27T17:28:28+00:00\" \/>\n<meta name=\"author\" content=\"\u6d77\u6597, \u8475\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u57f7\u7b46\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u6d77\u6597, \u8475\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/\",\"url\":\"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/\",\"name\":\"python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528 - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/#website\"},\"datePublished\":\"2024-03-21T14:57:50+00:00\",\"dateModified\":\"2024-03-27T17:28:28+00:00\",\"author\":{\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/20cfc053626f4d45c0fa7a4e7964b5b6\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/#breadcrumb\"},\"inLanguage\":\"ja\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.silicloud.com\/ja\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/ja\/blog\/\",\"name\":\"Blog - Silicon Cloud\",\"description\":\"\",\"inLanguage\":\"ja\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/20cfc053626f4d45c0fa7a4e7964b5b6\",\"name\":\"\u6d77\u6597, \u8475\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ja\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/25aec9a18954b6bfb7e4f7219c2923d62c6f0c9f4d5c0171228fe41751c0ab7a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/25aec9a18954b6bfb7e4f7219c2923d62c6f0c9f4d5c0171228fe41751c0ab7a?s=96&d=mm&r=g\",\"caption\":\"\u6d77\u6597, \u8475\"},\"url\":\"https:\/\/www.silicloud.com\/ja\/blog\/author\/kaitoaoi\/\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"ja\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/#local-main-organization-logo\",\"url\":\"\",\"contentUrl\":\"\",\"caption\":\"Blog - Silicon Cloud\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528 - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/ja\/blog\/python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528\/","og_locale":"ja_JP","og_type":"article","og_title":"python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528","og_description":"Python\u306emultiprocessing\u30e2\u30b8\u30e5\u30fc\u30eb\u306eLock\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3057\u3066\u30de\u30eb\u30c1\u30d7\u30ed\u30bb\u30b9\u30ed\u30c3\u30af\u3092\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002 \u591a\u30d7\u30ed\u30bb\u30b9\u306b\u304a\u3051\u308b\u30ed\u30c3\u30af\u306e\u7c21\u5358\u306a\u4f7f\u7528\u4f8b\u3092\u6b21\u306b\u793a\u3057\u307e\u3059\u3002 import multiprocessing [&hellip;]","og_url":"https:\/\/www.silicloud.com\/ja\/blog\/python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528\/","og_site_name":"Blog - Silicon Cloud","article_published_time":"2024-03-21T14:57:50+00:00","article_modified_time":"2024-03-27T17:28:28+00:00","author":"\u6d77\u6597, \u8475","twitter_card":"summary_large_image","twitter_misc":{"\u57f7\u7b46\u8005":"\u6d77\u6597, \u8475"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/","url":"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/","name":"python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528 - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/ja\/blog\/#website"},"datePublished":"2024-03-21T14:57:50+00:00","dateModified":"2024-03-27T17:28:28+00:00","author":{"@id":"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/20cfc053626f4d45c0fa7a4e7964b5b6"},"breadcrumb":{"@id":"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/#breadcrumb"},"inLanguage":"ja","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.silicloud.com\/ja\/blog\/"},{"@type":"ListItem","position":2,"name":"python\u591a\u8fdb\u7a0b\u9501\u600e\u4e48\u4f7f\u7528"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/ja\/blog\/#website","url":"https:\/\/www.silicloud.com\/ja\/blog\/","name":"Blog - Silicon Cloud","description":"","inLanguage":"ja"},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/20cfc053626f4d45c0fa7a4e7964b5b6","name":"\u6d77\u6597, \u8475","image":{"@type":"ImageObject","inLanguage":"ja","@id":"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/25aec9a18954b6bfb7e4f7219c2923d62c6f0c9f4d5c0171228fe41751c0ab7a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/25aec9a18954b6bfb7e4f7219c2923d62c6f0c9f4d5c0171228fe41751c0ab7a?s=96&d=mm&r=g","caption":"\u6d77\u6597, \u8475"},"url":"https:\/\/www.silicloud.com\/ja\/blog\/author\/kaitoaoi\/"},{"@type":"ImageObject","inLanguage":"ja","@id":"https:\/\/www.silicloud.com\/ja\/blog\/python%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%94%81%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8\/#local-main-organization-logo","url":"","contentUrl":"","caption":"Blog - Silicon Cloud"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/posts\/98726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/comments?post=98726"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/posts\/98726\/revisions"}],"predecessor-version":[{"id":198829,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/posts\/98726\/revisions\/198829"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/media?parent=98726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/categories?post=98726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/tags?post=98726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}