{"id":953614,"date":"2024-12-27T01:45:26","date_gmt":"2024-12-26T17:45:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/953614.html"},"modified":"2024-12-27T01:45:27","modified_gmt":"2024-12-26T17:45:27","slug":"python%e4%bd%a0%e5%a6%82%e4%bd%95%e6%8c%89%e7%85%a7gevent","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/953614.html","title":{"rendered":"python\u4f60\u5982\u4f55\u6309\u7167gevent"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094026\/ea0643c6-7a3a-42fc-9ccb-fb4d62132ffa-1.webp\" alt=\"python\u4f60\u5982\u4f55\u6309\u7167gevent\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528gevent\u5e93\u6765\u5b9e\u73b0\u5e76\u53d1\u7f16\u7a0b\u3001gevent\u662f\u4e00\u4e2a\u57fa\u4e8e\u534f\u7a0b\u7684Python\u7f51\u7edc\u5e93\u3001\u901a\u8fc7monkey patching\u4f7f\u5f97\u6807\u51c6\u5e93\u53ef\u4ee5\u534f\u4f5c\u5f0f\u5730\u5de5\u4f5c\u3002<\/strong> \u5176\u4e2d\u4e00\u4e2a\u5173\u952e\u6b65\u9aa4\u662f\u4f7f\u7528 <code>gevent<\/code> \u7684 <code>monkey<\/code> \u6a21\u5757\u6765\u8865\u4e01\u6807\u51c6\u5e93\uff0c\u4ee5\u4fbf\u4f7f\u5176\u4e0e <code>gevent<\/code> \u4e00\u8d77\u5de5\u4f5c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c<code>gevent<\/code> \u53ef\u4ee5\u63a5\u7ba1\u6807\u51c6\u5e93\u7684\u963b\u585e\u8c03\u7528\uff0c\u4ece\u800c\u5b9e\u73b0\u975e\u963b\u585e\u7684\u534f\u7a0b\u8c03\u5ea6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528 <code>gevent<\/code> \u6765\u8fdb\u884c\u5e76\u53d1\u7f16\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001GEVENT\u7b80\u4ecb<\/p>\n<\/p>\n<p><p><code>gevent<\/code> \u662f\u4e00\u4e2a\u57fa\u4e8e\u534f\u7a0b\u7684Python\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u7b80\u5316\u5e76\u53d1\u7f16\u7a0b\u3002\u5b83\u901a\u8fc7\u534f\u7a0b\u6765\u5b9e\u73b0\u5e76\u53d1\u5904\u7406\uff0c\u907f\u514d\u4e86\u4f20\u7edf\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u6240\u5e26\u6765\u7684\u590d\u6742\u6027\u3002\u534f\u7a0b\u662f\u8f7b\u91cf\u7ea7\u7684\uff0c\u5e76\u4e14\u5177\u6709\u4f4e\u5f00\u9500\u7684\u4f18\u70b9\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u91cf\u5e76\u53d1\u4efb\u52a1\u65f6\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u534f\u7a0b\u7684\u7279\u70b9<\/strong><\/p>\n<\/p>\n<p><p>\u534f\u7a0b\u662f\u6bd4\u7ebf\u7a0b\u66f4\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u5355\u5143\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u5355\u4e2a\u7ebf\u7a0b\u5185\u534f\u4f5c\u5730\u8fd0\u884c\u3002\u4e0e\u7ebf\u7a0b\u4e0d\u540c\uff0c\u534f\u7a0b\u4e0d\u9700\u8981\u64cd\u4f5c\u7cfb\u7edf\u7ea7\u522b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u56e0\u6b64\u5207\u6362\u901f\u5ea6\u66f4\u5feb\u3002\u6b64\u5916\uff0c\u534f\u7a0b\u7684\u5185\u5b58\u6d88\u8017\u4e5f\u66f4\u5c0f\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u4e00\u4e2a\u7ebf\u7a0b\u5185\u5171\u4eab\u6808\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>gevent\u7684\u57fa\u672c\u539f\u7406<\/strong><\/p>\n<\/p>\n<p><p><code>gevent<\/code> \u4f7f\u7528\u7eff\u8272\u7ebf\u7a0b\uff08\u4e5f\u79f0\u4e3a\u5fae\u7ebf\u7a0b\uff09\u6765\u5b9e\u73b0\u534f\u7a0b\u3002\u7eff\u8272\u7ebf\u7a0b\u7531\u7528\u6237\u6001\u8c03\u5ea6\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u3002<code>gevent<\/code> \u901a\u8fc7\u4e8b\u4ef6\u5faa\u73af\u6765\u8c03\u5ea6\u8fd9\u4e9b\u7eff\u8272\u7ebf\u7a0b\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u7ec4API\u6765\u521b\u5efa\u3001\u7ba1\u7406\u548c\u534f\u8c03\u534f\u7a0b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001GEVENT\u7684\u5b89\u88c5\u4e0e\u57fa\u7840\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528 <code>gevent<\/code> \u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u5728\u7ec8\u7aef\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5 <code>gevent<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install gevent<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528 <code>gevent<\/code> \u6765\u7f16\u5199\u5e76\u53d1\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u4f7f\u7528\u793a\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 <code>gevent<\/code> \u6765\u5e76\u53d1\u6267\u884c\u4e24\u4e2a\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gevent<\/p>\n<p>from gevent import monkey<\/p>\n<p>import time<\/p>\n<p>monkey.patch_all()<\/p>\n<p>def task1():<\/p>\n<p>    for i in range(3):<\/p>\n<p>        print(f&quot;Task 1: {i}&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>def task2():<\/p>\n<p>    for i in range(3):<\/p>\n<p>        print(f&quot;Task 2: {i}&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    g1 = gevent.spawn(task1)<\/p>\n<p>    g2 = gevent.spawn(task2)<\/p>\n<p>    gevent.joinall([g1, g2])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u4efb\u52a1 <code>task1<\/code> \u548c <code>task2<\/code>\uff0c\u5e76\u4f7f\u7528 <code>gevent.spawn<\/code> \u51fd\u6570\u542f\u52a8\u5b83\u4eec\u3002<code>gevent.joinall<\/code> \u51fd\u6570\u7528\u4e8e\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001MONKEY PATCHING\u7684\u4f5c\u7528\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p><code>monkey.patch_all()<\/code> \u662f <code>gevent<\/code> \u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\uff0c\u5b83\u901a\u8fc7\u8865\u4e01\u6807\u51c6\u5e93\u6765\u4f7f\u5176\u4e0e <code>gevent<\/code> \u534f\u4f5c\u3002\u8865\u4e01\u7684\u76ee\u7684\u662f\u5c06\u6807\u51c6\u5e93\u4e2d\u7684\u963b\u585e\u8c03\u7528\u8f6c\u6362\u4e3a\u975e\u963b\u585e\u8c03\u7528\uff0c\u4ee5\u4fbf <code>gevent<\/code> \u53ef\u4ee5\u63a5\u7ba1\u8c03\u5ea6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>monkey patching\u7684\u4f5c\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7 <code>monkey.patch_all()<\/code>\uff0c<code>gevent<\/code> \u53ef\u4ee5\u66ff\u6362\u6807\u51c6\u5e93\u4e2d\u4e0e\u7f51\u7edc\u3001\u6587\u4ef6I\/O\u3001\u7ebf\u7a0b\u7b49\u76f8\u5173\u7684\u6a21\u5757\uff0c\u4f7f\u5b83\u4eec\u652f\u6301\u534f\u7a0b\u3002\u4f8b\u5982\uff0c<code>time.sleep<\/code> \u88ab\u66ff\u6362\u4e3a <code>gevent.sleep<\/code>\uff0c\u4ece\u800c\u4f7f\u5f97\u5728\u534f\u7a0b\u4e2d\u8c03\u7528 <code>sleep<\/code> \u4e0d\u4f1a\u963b\u585e\u5176\u4ed6\u534f\u7a0b\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>monkey.patch_all()<\/code> \u65f6\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8865\u4e01\u987a\u5e8f<\/strong>\uff1a\u8865\u4e01\u64cd\u4f5c\u5e94\u5c3d\u65e9\u6267\u884c\uff0c\u6700\u597d\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u5c31\u8c03\u7528 <code>monkey.patch_all()<\/code>\uff0c\u4ee5\u786e\u4fdd\u6240\u6709\u5bfc\u5165\u7684\u6a21\u5757\u90fd\u662f\u7ecf\u8fc7\u8865\u4e01\u7684\u7248\u672c\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027\u95ee\u9898<\/strong>\uff1a\u67d0\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u80fd\u4e0d\u4e0e <code>gevent<\/code> \u7684\u8865\u4e01\u517c\u5bb9\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u8fd9\u4e9b\u5e93\u65f6\u9700\u8981\u8fdb\u884c\u6d4b\u8bd5\u548c\u9a8c\u8bc1\u3002<\/li>\n<li><strong>\u6027\u80fd\u5f71\u54cd<\/strong>\uff1a\u867d\u7136 <code>monkey patching<\/code> \u63d0\u4f9b\u4e86\u4fbf\u5229\u6027\uff0c\u4f46\u5b83\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u4e9b\u6027\u80fd\u4e0a\u7684\u5f00\u9500\uff0c\u5c24\u5176\u662f\u5728\u5927\u91cf\u8c03\u7528\u7684\u573a\u666f\u4e0b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001GEVENT\u4e2d\u7684\u534f\u7a0b\u7ba1\u7406<\/p>\n<\/p>\n<p><p><code>gevent<\/code> \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u7ba1\u7406\u548c\u534f\u8c03\u534f\u7a0b\u7684\u6267\u884c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>gevent.spawn<\/code> \u542f\u52a8\u534f\u7a0b\uff0c\u5e76\u4f7f\u7528 <code>gevent.joinall<\/code> \u7b49\u51fd\u6570\u6765\u7b49\u5f85\u534f\u7a0b\u5b8c\u6210\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u534f\u7a0b\u7684\u521b\u5efa\u4e0e\u542f\u52a8<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>gevent.spawn<\/code> \u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u548c\u542f\u52a8\u534f\u7a0b\u3002<code>spawn<\/code> \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff08\u5982\u51fd\u6570\uff09\u4ee5\u53ca\u8be5\u5bf9\u8c61\u7684\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a <code>Greenlet<\/code> \u5bf9\u8c61\u3002<code>Greenlet<\/code> \u662f <code>gevent<\/code> \u4e2d\u534f\u7a0b\u7684\u62bd\u8c61\uff0c\u4ee3\u8868\u4e00\u4e2a\u6b63\u5728\u6267\u884c\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_task(arg1, arg2):<\/p>\n<p>    pass<\/p>\n<p>g = gevent.spawn(my_task, arg1, arg2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u534f\u7a0b\u7684\u540c\u6b65\u4e0e\u7b49\u5f85<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u540c\u6b65\u534f\u7a0b\u7684\u6267\u884c\uff0c<code>gevent<\/code> \u63d0\u4f9b\u4e86 <code>gevent.joinall<\/code> \u548c <code>gevent.wait<\/code> \u7b49\u51fd\u6570\u3002<code>joinall<\/code> \u51fd\u6570\u7528\u4e8e\u7b49\u5f85\u591a\u4e2a\u534f\u7a0b\u5b8c\u6210\uff0c\u800c <code>wait<\/code> \u51fd\u6570\u5219\u7528\u4e8e\u7b49\u5f85\u5355\u4e2a\u534f\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">g1 = gevent.spawn(task1)<\/p>\n<p>g2 = gevent.spawn(task2)<\/p>\n<p>gevent.joinall([g1, g2])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001GEVENT\u4e2d\u7684\u5e76\u53d1\u6a21\u5f0f<\/p>\n<\/p>\n<p><p><code>gevent<\/code> \u652f\u6301\u591a\u79cd\u5e76\u53d1\u6a21\u5f0f\uff0c\u5305\u62ec\u4efb\u52a1\u8c03\u5ea6\u3001\u4e8b\u4ef6\u5904\u7406\u548c\u8d44\u6e90\u5171\u4eab\u7b49\u3002\u901a\u8fc7\u4e0d\u540c\u7684\u6a21\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u5e76\u53d1\u903b\u8f91\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4efb\u52a1\u8c03\u5ea6<\/strong><\/p>\n<\/p>\n<p><p><code>gevent<\/code> \u4f7f\u7528\u4e8b\u4ef6\u5faa\u73af\u6765\u8c03\u5ea6\u534f\u7a0b\u7684\u6267\u884c\u3002\u4e8b\u4ef6\u5faa\u73af\u4f1a\u76d1\u542c\u6240\u6709\u534f\u7a0b\u7684\u72b6\u6001\uff0c\u5e76\u5728\u534f\u7a0b\u9700\u8981\u7b49\u5f85\u6216\u5b8c\u6210\u65f6\u8fdb\u884c\u5207\u6362\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u534f\u7a0b\u90fd\u80fd\u5f97\u5230\u516c\u5e73\u7684\u6267\u884c\u673a\u4f1a\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e8b\u4ef6\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p><code>gevent<\/code> \u63d0\u4f9b\u4e86\u4e8b\u4ef6\u5bf9\u8c61\u6765\u5b9e\u73b0\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f16\u7a0b\u6a21\u578b\u3002\u4e8b\u4ef6\u5bf9\u8c61\u53ef\u4ee5\u7528\u4e8e\u534f\u8c03\u591a\u4e2a\u534f\u7a0b\u4e4b\u95f4\u7684\u6267\u884c\u987a\u5e8f\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>gevent.event.Event<\/code> \u6765\u5b9e\u73b0\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from gevent.event import Event<\/p>\n<p>evt = Event()<\/p>\n<p>def producer():<\/p>\n<p>    # \u751f\u4ea7\u6570\u636e<\/p>\n<p>    evt.set()  # \u901a\u77e5\u6d88\u8d39\u8005<\/p>\n<p>def consumer():<\/p>\n<p>    evt.wait()  # \u7b49\u5f85\u751f\u4ea7\u8005\u901a\u77e5<\/p>\n<p>    # \u6d88\u8d39\u6570\u636e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8d44\u6e90\u5171\u4eab<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u534f\u7a0b\u4e2d\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u8d44\u6e90\u7684\u540c\u6b65\u95ee\u9898\u3002<code>gevent<\/code> \u63d0\u4f9b\u4e86\u9501\u548c\u4fe1\u53f7\u91cf\u7b49\u540c\u6b65\u539f\u8bed\uff0c\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u5e76\u53d1\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>gevent.lock.Semaphore<\/code> \u6765\u9650\u5236\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from gevent.lock import Semaphore<\/p>\n<p>sem = Semaphore(2)<\/p>\n<p>def access_resource():<\/p>\n<p>    with sem:<\/p>\n<p>        # \u8bbf\u95ee\u5171\u4eab\u8d44\u6e90<\/p>\n<p>        pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001GEVENT\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p><code>gevent<\/code> \u9002\u7528\u4e8e\u591a\u79cd\u5e94\u7528\u573a\u666f\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u6216\u4efb\u52a1\u7684\u60c5\u51b5\u4e0b\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f51\u7edc\u670d\u52a1\u5668<\/strong><\/p>\n<\/p>\n<p><p><code>gevent<\/code> \u5e38\u7528\u4e8e\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u670d\u52a1\u5668\u3002\u901a\u8fc7\u534f\u7a0b\u6765\u5904\u7406\u7f51\u7edc\u8bf7\u6c42\uff0c<code>gevent<\/code> \u53ef\u4ee5\u8f7b\u677e\u5730\u5e94\u5bf9\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>gevent<\/code> \u6765\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from gevent.pywsgi import WSGIServer<\/p>\n<p>from my_application import app<\/p>\n<p>http_server = WSGIServer((&#39;0.0.0.0&#39;, 8080), app)<\/p>\n<p>http_server.serve_forever()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u722c\u866b<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6784\u5efa\u7f51\u7edc\u722c\u866b\u65f6\uff0c<code>gevent<\/code> \u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u63d0\u9ad8\u6293\u53d6\u901f\u5ea6\u3002\u901a\u8fc7\u5e76\u53d1\u5730\u53d1\u9001\u591a\u4e2a\u8bf7\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8f83\u77ed\u65f6\u95f4\u5185\u6293\u53d6\u5927\u91cf\u7f51\u9875\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gevent<\/p>\n<p>from gevent import monkey<\/p>\n<p>import requests<\/p>\n<p>monkey.patch_all()<\/p>\n<p>def fetch(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    print(url, response.status_code)<\/p>\n<p>urls = [&#39;http:\/\/example.com&#39;, &#39;http:\/\/example.org&#39;, &#39;http:\/\/example.net&#39;]<\/p>\n<p>jobs = [gevent.spawn(fetch, url) for url in urls]<\/p>\n<p>gevent.joinall(jobs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u65f6\u901a\u4fe1<\/strong><\/p>\n<\/p>\n<p><p><code>gevent<\/code> \u4e5f\u5e38\u7528\u4e8e\u5b9e\u73b0\u5b9e\u65f6\u901a\u4fe1\u5e94\u7528\uff0c\u5982\u804a\u5929\u670d\u52a1\u5668\u3001WebSocket\u7b49\u3002\u901a\u8fc7\u534f\u7a0b\u7684\u975e\u963b\u585e\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u9ad8\u5e76\u53d1\u73af\u5883\u4e0b\u5b9e\u73b0\u4f4e\u5ef6\u8fdf\u7684\u5b9e\u65f6\u901a\u4fe1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001GEVENT\u4e0e\u5176\u4ed6\u5e76\u53d1\u6a21\u578b\u7684\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u9664\u4e86 <code>gevent<\/code> \u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e38\u7528\u7684\u5e76\u53d1\u6a21\u578b\uff0c\u5982\u7ebf\u7a0b\u3001\u8fdb\u7a0b\u548casyncio\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u6bd4\u8f83 <code>gevent<\/code> \u4e0e\u8fd9\u4e9b\u6a21\u578b\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e0e\u7ebf\u7a0b\u7684\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u534f\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u6bd4\u7ebf\u7a0b\u66f4\u5feb\uff0c\u56e0\u6b64\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c<code>gevent<\/code> \u901a\u5e38\u6bd4\u7ebf\u7a0b\u66f4\u9ad8\u6548\u3002<\/li>\n<li><strong>\u8d44\u6e90\u6d88\u8017<\/strong>\uff1a\u534f\u7a0b\u7684\u5185\u5b58\u6d88\u8017\u8f83\u4f4e\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u5355\u4e2a\u7ebf\u7a0b\u5185\u8fd0\u884c\uff0c\u800c\u7ebf\u7a0b\u9700\u8981\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u5206\u914d\u72ec\u7acb\u7684\u6808\u3002<\/li>\n<li><strong>\u7f16\u7a0b\u6a21\u578b<\/strong>\uff1a<code>gevent<\/code> \u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\uff0c\u907f\u514d\u4e86\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u7684\u9501\u3001\u6761\u4ef6\u53d8\u91cf\u7b49\u590d\u6742\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4e0e\u8fdb\u7a0b\u7684\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u9694\u79bb\u6027<\/strong>\uff1a\u8fdb\u7a0b\u95f4\u662f\u5b8c\u5168\u9694\u79bb\u7684\uff0c\u56e0\u6b64\u5728\u9700\u8981\u9ad8\u9694\u79bb\u6027\u65f6\uff0c\u8fdb\u7a0b\u6a21\u578b\u66f4\u5408\u9002\u3002<\/li>\n<li><strong>\u5f00\u9500<\/strong>\uff1a\u8fdb\u7a0b\u7684\u521b\u5efa\u548c\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u8f83\u5927\uff0c\u56e0\u6b64\u5728\u9700\u8981\u5927\u91cf\u5e76\u53d1\u65f6\uff0c<code>gevent<\/code> \u66f4\u5177\u4f18\u52bf\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4e0easyncio\u7684\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u6613\u7528\u6027<\/strong>\uff1a<code>gevent<\/code> \u7684API\u8bbe\u8ba1\u66f4\u7b80\u5355\uff0c\u5c24\u5176\u9002\u5408\u7f51\u7edc\u7f16\u7a0b\uff0c\u800c <code>asyncio<\/code> \u9700\u8981\u4f7f\u7528 <code>async<\/code> \u548c <code>await<\/code> \u5173\u952e\u5b57\u3002<\/li>\n<li><strong>\u751f\u6001\u7cfb\u7edf<\/strong>\uff1a<code>asyncio<\/code> \u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5177\u6709\u66f4\u5e7f\u6cdb\u7684\u652f\u6301\u548c\u751f\u6001\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u516b\u3001GEVENT\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528 <code>gevent<\/code> \u8fdb\u884c\u5e76\u53d1\u7f16\u7a0b\u65f6\uff0c\u6211\u4eec\u9700\u8981\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u9ad8\u6548\u548c\u7a33\u5b9a\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5408\u7406\u4f7f\u7528monkey patching<\/strong><\/p>\n<\/p>\n<p><p><code>monkey patching<\/code> \u867d\u7136\u65b9\u4fbf\uff0c\u4f46\u4e5f\u53ef\u80fd\u5f15\u5165\u517c\u5bb9\u6027\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5728\u7a0b\u5e8f\u5f00\u59cb\u65f6\u5c3d\u65e9\u6267\u884c\u8865\u4e01\u64cd\u4f5c\uff0c\u5e76\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u5145\u5206\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u534f\u7a0b\u7684\u8bbe\u8ba1<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1\u534f\u7a0b\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5c3d\u91cf\u907f\u514d\u963b\u585e\u64cd\u4f5c\uff0c\u4ee5\u5145\u5206\u5229\u7528 <code>gevent<\/code> \u7684\u5e76\u53d1\u80fd\u529b\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>gevent.sleep(0)<\/code> \u6765\u4e3b\u52a8\u8ba9\u51fa\u6267\u884c\u6743\uff0c\u4ee5\u63d0\u9ad8\u4e8b\u4ef6\u5faa\u73af\u7684\u8c03\u5ea6\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76d1\u63a7\u4e0e\u8c03\u8bd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9ad8\u5e76\u53d1\u73af\u5883\u4e0b\uff0c\u95ee\u9898\u7684\u6392\u67e5\u548c\u8c03\u8bd5\u53ef\u80fd\u53d8\u5f97\u56f0\u96be\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>gevent<\/code> \u63d0\u4f9b\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u5982 <code>gevent.util<\/code> \u548c <code>gevent.threadpool<\/code>\uff0c\u6765\u76d1\u63a7\u534f\u7a0b\u7684\u6267\u884c\u72b6\u6001\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u6211\u4eec\u53ef\u4ee5\u5145\u5206\u53d1\u6325 <code>gevent<\/code> \u7684\u4f18\u52bf\uff0c\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u5e76\u53d1\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528gevent\u63d0\u9ad8Python\u5e94\u7528\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\uff1f<\/strong><br \/>\u4f7f\u7528gevent\u53ef\u4ee5\u901a\u8fc7\u534f\u4f5c\u5f0f\u591a\u4efb\u52a1\u5904\u7406\u6765\u63d0\u5347Python\u5e94\u7528\u7684\u5e76\u53d1\u6027\u80fd\u3002\u5b83\u57fa\u4e8e\u534f\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af\u6a21\u578b\uff0c\u5141\u8bb8\u4f60\u5728\u5355\u7ebf\u7a0b\u4e2d\u540c\u65f6\u5904\u7406\u591a\u4e2a\u4efb\u52a1\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528gevent\u7684\u7eff\u7ebf\u7a0b\uff08Greenlet\uff09\uff0c\u901a\u8fc7\u7334\u5b50\u8865\u4e01\uff08monkey patching\uff09\u66ff\u6362\u6807\u51c6\u5e93\u7684\u963b\u585e\u8c03\u7528\uff0c\u786e\u4fdd\u7f51\u7edcI\/O\u7b49\u64cd\u4f5c\u4e0d\u4f1a\u963b\u585e\u4e8b\u4ef6\u5faa\u73af\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u6548\u7684\u5e76\u53d1\u6267\u884c\u3002<\/p>\n<p><strong>gevent\u4e0e\u5176\u4ed6\u5e76\u53d1\u5e93\uff08\u5982threading\u548casyncio\uff09\u76f8\u6bd4\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>gevent\u7684\u4e3b\u8981\u4f18\u52bf\u5728\u4e8e\u5176\u7b80\u5355\u6613\u7528\u6027\u548c\u9ad8\u6027\u80fd\u3002\u4e0ethreading\u5e93\u76f8\u6bd4\uff0cgevent\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u66f4\u5c0f\uff0c\u80fd\u591f\u6709\u6548\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\u3002\u6b64\u5916\uff0c\u4e0easyncio\u76f8\u6bd4\uff0cgevent\u7684\u7f16\u7a0b\u6a21\u578b\u66f4\u4e3a\u76f4\u89c2\uff0c\u9002\u5408\u4f20\u7edf\u963b\u585e\u5f0f\u7f16\u7a0b\u7684\u8f6c\u5316\uff0c\u7279\u522b\u662f\u5f53\u6d89\u53ca\u5230\u7f51\u7edc\u548cI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\uff0c\u4f7f\u7528gevent\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528gevent\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5f02\u5e38\u548c\u9519\u8bef\uff1f<\/strong><br \/>\u5728gevent\u4e2d\u5904\u7406\u5f02\u5e38\u7684\u65b9\u5f0f\u4e0e\u666e\u901aPython\u4ee3\u7801\u76f8\u4f3c\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002\u7531\u4e8egevent\u7684\u534f\u7a0b\u662f\u72ec\u7acb\u8fd0\u884c\u7684\uff0c\u67d0\u4e2a\u534f\u7a0b\u4e2d\u7684\u5f02\u5e38\u4e0d\u4f1a\u5f71\u54cd\u5230\u5176\u4ed6\u534f\u7a0b\u7684\u6267\u884c\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u6bcf\u4e2a\u534f\u7a0b\u4e2d\u90fd\u5b9e\u73b0\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u5065\u58ee\u6027\u3002\u540c\u65f6\uff0c\u4f7f\u7528gevent\u63d0\u4f9b\u7684<code>join()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u534f\u7a0b\u5b8c\u6210\u540e\u518d\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528gevent\u5e93\u6765\u5b9e\u73b0\u5e76\u53d1\u7f16\u7a0b\u3001gevent\u662f\u4e00\u4e2a\u57fa\u4e8e\u534f\u7a0b\u7684Python\u7f51\u7edc\u5e93\u3001\u901a\u8fc7m [&hellip;]","protected":false},"author":3,"featured_media":953615,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953614"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=953614"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953614\/revisions"}],"predecessor-version":[{"id":953616,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953614\/revisions\/953616"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/953615"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=953614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=953614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=953614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}