{"id":47118,"date":"2024-12-02T08:38:17","date_gmt":"2024-12-02T00:38:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47118\/"},"modified":"2024-12-02T08:38:17","modified_gmt":"2024-12-02T00:38:17","slug":"php%e5%87%bd%e6%95%b0%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e5%ae%9e%e6%88%98%ef%bc%9a%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98%e8%a7%a3%e5%86%b3%e4%b9%8b%e9%81%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47118\/","title":{"rendered":"PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u4e4b\u9053"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u4e4b\u9053<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u4e4b\u9053\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p>PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5141\u8bb8\u5728\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u8fd0\u884c\u51fd\u6570\uff0c\u4f46\u4f1a\u9047\u5230\u5e38\u89c1\u95ee\u9898\u3002\u8fd9\u4e9b\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u6307\u5357\u5305\u62ec\uff1a\u6b7b\u9501\uff1a\u4f7f\u7528\u6b7b\u9501\u68c0\u6d4b\u673a\u5236\uff0c\u5982\u8d85\u65f6\u6216\u91cd\u65b0\u83b7\u53d6\u9501\u3002\u5185\u5b58\u6cc4\u6f0f\uff1a\u4f7f\u7528\u95ed\u5305\u6216\u533f\u540d\u51fd\u6570\u9632\u6b62\u5916\u90e8\u53d8\u91cf\u5f15\u7528\u3002\u7ade\u4e89\u6761\u4ef6\uff1a\u4f7f\u7528\u9501\u6216\u4e92\u65a5\u4f53\u786e\u4fdd\u539f\u5b50\u6570\u636e\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a$mutex\u7684\u4e92\u65a5\u4f53\uff0c\u5e76\u5728\u8bbf\u95ee\u4e34\u754c\u533a\u4ee3\u7801\u65f6\u52a0\u9501\u89e3\u9501\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729866817671bac4181703.jpg\" class=\"aligncenter\" title=\"PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u4e4b\u9053\u63d2\u56fe\" alt=\"PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u4e4b\u9053\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u4e4b\u9053<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u60a8\u5728\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u8fd0\u884c\u591a\u4e2a\u51fd\u6570\u3002\u8fd9\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u67d0\u4e9b\u7c7b\u578b\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<p><strong>\u5e38\u89c1\u95ee\u9898<\/strong><\/p>\n<p>\u867d\u7136PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5f88\u6709\u7528\uff0c\u4f46\u5b83\u4e5f\u53ef\u80fd\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\u6307\u5357\uff1a<\/p>\n<p><strong>\u95ee\u9898 1\uff1a\u6b7b\u9501<\/strong><\/p>\n<p><strong>\u539f\u56e0\uff1a<\/strong>\u5f53\u4e24\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u7b49\u5f85\u5f7c\u6b64\u91ca\u653e\u9501\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u6b7b\u9501\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong>\u4f7f\u7528\u6b7b\u9501\u68c0\u6d4b\u673a\u5236\uff0c\u4f8b\u5982\u8d85\u65f6\u6216\u5c1d\u8bd5\u91cd\u65b0\u83b7\u53d6\u9501\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u5c06\u7b2c\u4e00\u4e2a\u51fd\u6570\u6807\u8bb0\u4e3a\u53ef\u91cd\u5165\ndeclare(ticks = 1);\n\nfunction func1() {\n    $lock = fopen('lock.txt', 'w');\n    stream_set_blocking($lock, FALSE);\n\n    \/\/ \u5c1d\u8bd5\u52a0\u9501\n    $locked = flock($lock, LOCK_EX);\n\n    \/\/ \u5982\u679c\u9501\u5b9a\u5931\u8d25\uff0c\u5219\u7b49\u5f85\n    while (!$locked) {\n        if (time() - $start &gt; 30) {\n            \/\/ \u8d85\u65f6\uff0c\u91ca\u653e\u9501\n            flock($lock, LOCK_UN);\n            return false;\n        }\n        usleep(100000);\n        $locked = flock($lock, LOCK_EX);\n    }\n\n    \/\/ \u6267\u884c\u4ee3\u7801...\n\n    flock($lock, LOCK_UN);\n}\n\nfunction func2() {\n    $lock = fopen('lock.txt', 'w');\n    stream_set_blocking($lock, FALSE);\n\n    \/\/ \u5c1d\u8bd5\u52a0\u9501\n    $locked = flock($lock, LOCK_EX);\n\n    \/\/ \u5982\u679c\u9501\u5b9a\u5931\u8d25\uff0c\u5219\u7b49\u5f85\n    while (!$locked) {\n        if (time() - $start &gt; 30) {\n            \/\/ \u8d85\u65f6\uff0c\u91ca\u653e\u9501\n            flock($lock, LOCK_UN);\n            return false;\n        }\n        usleep(100000);\n        $locked = flock($lock, LOCK_EX);\n    }\n\n    \/\/ \u6267\u884c\u4ee3\u7801...\n\n    flock($lock, LOCK_UN);\n}\n\nparallel([func1, func2]);<\/pre>\n<p><strong>\u95ee\u9898 2\uff1a\u5185\u5b58\u6cc4\u6f0f<\/strong><\/p>\n<p><strong>\u539f\u56e0\uff1a<\/strong>\u5f53\u4e00\u4e2a\u51fd\u6570\u4fdd\u6301\u5bf9\u53e6\u4e00\u4e2a\u51fd\u6570\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u7684\u5f15\u7528\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong>\u4f7f\u7528\u95ed\u5305\u6216\u533f\u540d\u51fd\u6570\u6765\u9632\u6b62\u5916\u90e8\u53d8\u91cf\u5f15\u7528\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>function func1() {\n    $variable = 'value';\n\n    parallel(function() use ($variable) {\n        \/\/ \u4f7f\u7528 $variable\n    });\n}<\/pre>\n<p><strong>\u95ee\u9898 3\uff1a\u7ade\u4e89\u6761\u4ef6<\/strong><\/p>\n<p><strong>\u539f\u56e0\uff1a<\/strong>\u5f53\u4e24\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u6570\u636e\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u7ade\u4e89\u6761\u4ef6\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong>\u4f7f\u7528\u9501\u6216\u4e92\u65a5\u4f53\u6765\u786e\u4fdd\u539f\u5b50\u6570\u636e\u8bbf\u95ee\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u521b\u5efa\u4e92\u65a5\u4f53\n$mutex = new Mutex();\n\nfunction func1() {\n    global $mutex;\n    \n    $mutex-&gt;lock();\n    \/\/ \u4e34\u754c\u533a\u4ee3\u7801...\n    $mutex-&gt;unlock();\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u4e4b\u9053\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u4e4b\u9053 \u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u4e4b\u9053\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5141\u8bb8\u5728\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u8fd0\u884c\u51fd\u6570\uff0c\u4f46\u4f1a\u9047\u5230\u5e38\u89c1\u95ee\u9898\u3002\u8fd9\u4e9b\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u6307\u5357\u5305\u62ec\uff1a\u6b7b\u9501\uff1a\u4f7f\u7528\u6b7b\u9501\u68c0\u6d4b\u673a\u5236\uff0c\u5982\u8d85\u65f6\u6216\u91cd\u65b0\u83b7\u53d6\u9501\u3002\u5185\u5b58\u6cc4\u6f0f\uff1a\u4f7f\u7528\u95ed\u5305\u6216\u533f\u540d\u51fd\u6570\u9632\u6b62\u5916\u90e8\u53d8\u91cf\u5f15\u7528\u3002\u7ade\u4e89\u6761\u4ef6\uff1a\u4f7f\u7528\u9501\u6216\u4e92\u65a5\u4f53\u786e\u4fdd\u539f\u5b50\u6570\u636e\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a$mutex\u7684\u4e92\u65a5\u4f53\uff0c\u5e76\u5728\u8bbf\u95ee\u4e34\u754c\u533a\u4ee3\u7801\u65f6\u52a0\u9501\u89e3\u9501\u3002 PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\uff1a\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u4e4b\u9053 \u7b80\u4ecb PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u662f\u4e00\u79cd\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u60a8\u5728\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u8fd0\u884c\u591a\u4e2a\u51fd\u6570\u3002\u8fd9\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u67d0\u4e9b\u7c7b\u578b\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002 \u5e38\u89c1\u95ee\u9898 \u867d\u7136PHP\u51fd\u6570\u5e76\u53d1\u7f16\u7a0b\u5f88\u6709\u7528\uff0c\u4f46\u5b83\u4e5f\u53ef\u80fd\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\u6307\u5357\uff1a \u95ee\u9898 1\uff1a\u6b7b\u9501 \u539f\u56e0\uff1a\u5f53\u4e24\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u7b49\u5f85\u5f7c\u6b64\u91ca\u653e\u9501\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u6b7b\u9501\u3002 \u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528\u6b7b\u9501\u68c0\u6d4b\u673a\u5236\uff0c\u4f8b\u5982\u8d85\u65f6\u6216\u5c1d\u8bd5\u91cd\u65b0\u83b7\u53d6\u9501\u3002 \u793a\u4f8b\uff1a \/\/ \u5c06\u7b2c\u4e00\u4e2a\u51fd\u6570\u6807\u8bb0\u4e3a\u53ef\u91cd\u5165 declare(ticks = 1); function func1() { $lock = fopen(&#8216;lock.txt&#8217;, &#8216;w&#8217;); stream_set_blocking($lock, FALSE); \/\/ \u5c1d\u8bd5\u52a0\u9501 $locked = flock($lock, LOCK_EX); \/\/ \u5982\u679c\u9501\u5b9a\u5931\u8d25\uff0c\u5219\u7b49\u5f85 while (!$locked) { if (time() &#8211; $start &gt; 30) { \/\/ \u8d85\u65f6\uff0c\u91ca\u653e\u9501 flock($lock, LOCK_UN); return false; } usleep(100000); $locked = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47118","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47118"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47118\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}