{"id":100351,"date":"2020-05-03T05:12:39","date_gmt":"2020-05-02T21:12:39","guid":{"rendered":"http:\/\/4563.org\/?p=100351"},"modified":"2020-05-03T05:12:39","modified_gmt":"2020-05-02T21:12:39","slug":"%e8%af%b7%e6%95%99%e4%b8%aa-python-%e4%b8%8d%e5%90%8c-py-%e6%96%87%e4%bb%b6%e4%b8%ad%e7%9a%84%e7%b1%bb%e8%8e%b7%e5%8f%96%e5%8f%a6%e4%b8%80%e4%b8%aa%e7%b1%bb%e5%ae%9e%e4%be%8b%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=100351","title":{"rendered":"\u8bf7\u6559\u4e2a Python \u4e0d\u540c py \u6587\u4ef6\u4e2d\u7684\u7c7b\u83b7\u53d6\u53e6\u4e00\u4e2a\u7c7b\u5b9e\u4f8b\u53d8\u91cf\u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e2a Python \u4e0d\u540c py \u6587\u4ef6\u4e2d\u7684\u7c7b\u83b7\u53d6\u53e6\u4e00\u4e2a\u7c7b\u5b9e\u4f8b\u53d8\u91cf\u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiaolinjia <\/span>  <span><i><\/i> 60<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6807\u9898\u611f\u89c9\u53d6\u7684\u602a\u602a\u7684\uff0c\u6240\u4ee5\u8fd8\u662f\u7528\u4ee3\u7801\u4e3e\u4e2a\u4f8b\u5b50\u5427\u3002<\/p>\n<p>\u76ee\u524d\u6709\u4e09\u4e2a py \u6587\u4ef6\uff0c\u5f53\u7136\u8fd9\u91cc\u7b80\u5316\u4e86\u3002\u5176\u4e2d test2.py \u662f\u7b2c\u4e09\u65b9\u5e93\u7684\u3002<\/p>\n<p>test1.py :<\/p>\n<pre><code>import threading from test2 import A  testA = A(1)   class B:     def __init__(self):         self._lock = threading.Lock()      def do_something_1(self):          def callback():             pass          testA.do_something(callback)   <\/code><\/pre>\n<p>test2.py<\/p>\n<pre><code>class A:     def __init__(self, param1):         self.a = param1         # \u521d\u59cb\u5316\u5f88\u591a\u4e1c\u897f\uff0c\u52a0\u8f7d\u4e9b\u6a21\u578b      def do_something(self, callback_func):         while True:             # \u60f3\u5728 reading \u4e4b\u524d\uff0c\u83b7\u53d6\u5230 test1.py \u4e2d\u5b9e\u4f8b\u7684\u9501\uff0c\u8fd9\u4e2a\u53ef\u4ee5\u505a\u5230\u5417             # reading             callback_func()  <\/code><\/pre>\n<p>test3.py<\/p>\n<pre><code>from flask import Flask, jsonify from test1 import B  app = Flask(__name__)   @app.route('\/xxx\/') def test1():     b = B()     b.start()     return jsonify({'msg': 'b has started.'})   if __name__ == '__main__':     app.run() <\/code><\/pre>\n<p>\u7136\u540e\u73b0\u5728\u60f3\u5728 test2.py \u4e2d\u7684 reading \u524d\u83b7\u53d6\u5230 B \u7c7b\u7684\u5b9e\u4f8b\u4e2d\u7684 lock \u6210\u5458\uff0c\u6216\u8005\u5176\u4ed6\u6210\u5458\uff0c\u8fd9\u4e2a\u8981\u600e\u4e48\u505a\u5230\u554a\uff0c\u6211\u60f3\u534a\u5929\u90fd\u4e0d\u77e5\u9053\u8981\u600e\u4e48\u624d\u83b7\u53d6\u7684\u5230\uff0c\u7528 globals()\u548c locals()\u770b\u4e86\u90fd\u6ca1\u6709\u3002\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"779897\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xiaolinjia <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e4b\u524d\u60f3\u5230\u7684\u4e00\u4e2a\u65b9\u6cd5\u662f\uff0c\u7ee7\u627f\u4e0b\u7b2c\u4e09\u65b9\u5e93\u7684 A \u7c7b\uff0c\u7136\u540e\u91cd\u8f7d\u4e0b do_something \u65b9\u6cd5\uff0c\u589e\u52a0\u591a\u4e00\u4e2a\u53c2\u6570\u6bd4\u5982 before\u3002<br \/>\u7136\u540e\u5728 B \u7c7b\u7684 do_something1()\u4e2d\uff0ctestA.do_something(callback, before=self)\u3002\u8fd9\u6837\u5c31\u80fd\u5728 A \u7c7b\u7684 do_something()\u4e2d\u83b7\u53d6\u5230 A \u5b9e\u4f8b\u7684\u6210\u5458\u7684\u503c\uff0c\u4e0d\u8fc7\u4e2a\u4eba\u611f\u89c9\u4e0d\u592a\u4f18\u96c5\u3002\u5c31\u5f53\u629b\u7816\u5f15\u7389\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"779898\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bigNewsMaker <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u80fd\u628a A \u7c7b\u5b9a\u4e49\u4e3a\u7ee7\u627f B \u7c7b\u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e2a Python \u4e0d\u540c py &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/100351"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=100351"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/100351\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100351"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}