{"id":192558,"date":"2020-11-11T07:56:58","date_gmt":"2020-11-10T23:56:58","guid":{"rendered":"http:\/\/4563.org\/?p=192558"},"modified":"2020-11-11T07:56:58","modified_gmt":"2020-11-10T23:56:58","slug":"%e5%85%b3%e4%ba%8e-django-%e7%9a%84%e4%b9%90%e8%a7%82%e9%94%81%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=192558","title":{"rendered":"\u5173\u4e8e Django \u7684\u4e50\u89c2\u9501\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e Django \u7684\u4e50\u89c2\u9501\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : IurNusRay <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        Django \u7684\u4e50\u89c2\u9501\u771f\u7684\u6709\u4f5c\u7528\u5417\uff1f\u6211\u5199\u4e86\u4e2a\u4f8b\u5b50\uff0c\u7528 jmeter \u5f00\u542f 1000 \u4e2a\u7ebf\u7a0b\u53bb\u8bf7\u6c42\uff0c\u7ed3\u679c\u5e76\u4e0d\u80fd\u89e3\u51b3\u8d44\u6e90\u7ade\u4e89\u7684\u95ee\u9898\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<br \/>with transaction.atomic():<br \/> save_id = transaction.savepoint()<br \/> try:<br \/> book = BookInfo.objects.get(id=1)<br \/> origin_read = book.read<br \/> new_read = origin_read + 1<br \/> BookInfo.objects.filter(id=book.id, read=origin_read).update(read=new_read)<br \/> book.save()<br \/> except Exception as e:<br \/> print(e)<br \/> transaction.savepoint_rollback(save_id)<\/p>\n<p> transaction.savepoint_commit(save_id)<\/p>\n<p>\u7406\u8bba\u4e0a\uff0cbook \u8868\u7684 read \u8d44\u6e90\u5728 1000 \u6b21\u8bf7\u6c42\u4e4b\u540e\u5e94\u8be5\u4f1a\u4ece 0 \u589e\u52a0\u5230 1000\uff0c\u7136\u800c\u5b9e\u9645\u4e0a\u53ea\u6709 500 \u5de6\u53f3\uff0c\u5f53\u6211\u964d\u4f4e\u7ebf\u7a0b\u6570\u91cf\u5230 100\uff0c\u4e5f\u53ea\u80fd\u5230 78\uff0c\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4157982\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nonduality <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u5728 try \u91cc\u5934\u52a0\u4e00\u884c\u8f93\u51fa log\uff0c\u770b\u6267\u884c\u60c5\u51b5\uff0c\u4e5f\u8bb8\u662f\u6709\u4e9b\u8bf7\u6c42 miss \u4e86\uff08\u7528\u5185\u7f6e\u5f00\u53d1\u670d\u52a1\u5668\u7684\u8bdd\u53ef\u80fd\u6027\u5f88\u5927\uff09\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u4f60\u53ef\u4ee5\u7528 F \u8868\u8fbe\u5f0f\uff0c\u6309\u8bf4\u662f\u53ef\u4ee5\u907f\u514d race condition \u95ee\u9898\uff1a<\/p>\n<p>&#8220;`python<br \/>try:<br \/> book = BookInfo.objects.get(id=1)<br \/> BookInfo.objects.filter(id=book.id, read=origin_read).update(read=F(&#8216;read&#8217;)+1)<br \/> book.save()<br \/>except Exception as e:<br \/> &#8230;.<\/p>\n<p>&#8220;`                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157983\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nonduality <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             try \u91cc\u5934\u6539\u6210\u8fd9\u4e24\u884c\u5dee\u4e0d\u591a\u5c31\u884c<br \/>book = BookInfo.objects.get(id=1)<br \/>BookInfo.objects.filter(id=book.pk).update(read=F(&#8216;read&#8217;)+1)<br \/>\u4f7f\u7528 F \u8868\u8fbe\u5f0f\u540e\uff0c\u4e0d\u786e\u5b9a\u8fd8\u9700\u4e0d\u9700\u8981\u4f7f\u7528 transaction\uff0c\u4f60\u53ef\u4ee5\u6d4b\u8bd5\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157984\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : IurNusRay <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nonduality F \u8868\u8fbe\u5f0f\u6211\u4e5f\u8bd5\u8fc7\uff0c\u4e5f\u4e0d\u884c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157985\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : IurNusRay <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e4b\u524d\u662f\u7528 runserver \u8fd0\u884c\u7684\uff0c\u53ef\u80fd\u5e76\u53d1\u652f\u6301\u4e0d\u884c\uff0c\u521a\u521a\u6362\u6210 uwsgi \u8fd0\u884c\uff0c\u53d1\u73b0\u7ed3\u679c\u5982\u4e0b\uff1a<br \/>1. book.read += 1 \u8fd9\u79cd\u65b9\u5f0f\u65e0\u6cd5\u89e3\u51b3\u8d44\u6e90\u7ade\u4e89\uff0c\u5b9e\u6d4b 1000 \u6b21\u8bf7\u6c42\uff0c\u53ea\u80fd\u52a0\u5230 500 \u5de6\u53f3<br \/>2. book.read = F(&#8220;read&#8221;) + 1 \u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u89e3\u51b3\uff0c\u5b9e\u6d4b 1000 \u6b21\u5e76\u53d1\u8bf7\u6c42\uff0c\u5206 5 \u6279\uff0c\u6700\u540e read \u503c\u52a0\u5230\u4e86 5000<br \/>3. book = BookInfo.objects.get(id=1)<br \/> origin_read = book.read<br \/> BookInfo.objects.filter(id=book.id, read=origin_read).update(read=origin_read + 1)<br \/> \u8fd9\u79cd\u6240\u8c13\u201c\u4e50\u89c2\u9501\u201c\u7684\u65b9\u5f0f\uff0c\u5b9e\u6d4b\u5b8c\u5168\u65e0\u6548\uff0c1000 \u6b21\u8bf7\u6c42\uff0cread \u503c\u53ea\u80fd\u52a0\u5230 500 \u5de6\u53f3<\/p>\n<p>\u7efc\u4e0a\uff0c\u4f7f\u7528 F \u8868\u8fbe\u5f0f\u662f\u6700\u6709\u6548\u7684\u65b9\u5f0f\uff0c\u4e0d\u662f\u5f88\u660e\u767d\u8fd9\u79cd\u4e50\u89c2\u9501\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff0c\u65e2\u6ca1\u6709\u89e3\u51b3\u8d44\u6e90\u7ade\u4e89\uff0c\u5b9e\u9645\u8fd0\u884c\u4e5f\u6ca1\u6709\u4efb\u4f55\u62a5\u9519                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157986\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nonduality <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @IurNusRay \u6211\u521a\u6d4b\u8bd5\u4e86\u4e00\u4e0b\uff0c\u7528 F \u8868\u8fbe\u5f0f\u8fdb\u884c\u6570\u636e\u81ea\u52a0\uff0cgunicorn \u8d77 1 \u4e2a\u8fdb\u7a0b\u8dd1\uff0c\u7528 ab -n 1000 -c 100 \u6d4b\u8bd5\uff0c\u5b8c\u5168\u6ca1\u95ee\u9898\u3002\u6240\u4ee5\uff0c\u7528 F \u8868\u8fbe\u5f0f\u5bf9\u591a\u6570\u60c5\u5f62\u4e0b\u662f\u591f\u7528\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157987\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nonduality <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u4e0d\u592a\u6e05\u695a\u8c01\u63d0\u51fa\u6765\u201c\u4e50\u89c2\u9501\u201d\uff0c\u4f46\u770b\u5176\u5b9e\u73b0\uff0c\u5927\u6982\u662f\u8981\u4fdd\u8bc1 filter \u5230\u7684\u5b9e\u4f8b\u72b6\u6001\u5177\u5907 origin_read \u7684\u503c\uff0c\u5728\u6b64\u57fa\u7840\u4e0a update \u6570\u503c\uff0c\u4f46\u5c31\u7b97\u6709 trasaction\uff0c\u4e5f\u65e0\u6cd5\u4fdd\u8bc1\u5b83\u4e00\u5b9a\u83b7\u53d6\u5230\u4f60\u8981\u7684\u6570\u636e\u72b6\u6001\uff0c\u81ea\u7136\u5c31 miss \u6389\u4e86\u3002\u4f46 F \u8868\u8fbe\u5f0f\u4e0d\u4e00\u6837\uff0c\u5b83\u7528 SQL \u8bed\u53e5\u5728\u6570\u636e\u5e93\u5c42\u9762\u76f4\u63a5\u64cd\u4f5c\u6570\u636e\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157988\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : IurNusRay <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nonduality \u521a\u521a\u6709\u770b\u4e86\u4e00\u4e0b\uff0c\u539f\u6765\u662f\u6211\u4ee3\u7801\u6f0f\u6389\u4e86\u4e00\u90e8\u5206\uff0c\u8fd9\u4e2a&#8221;\u4e50\u89c2\u9501&#8221;\u7684\u539f\u7406\u662f\u8981\u5f00\u542f\u4e00\u4e2a\u5faa\u73af\uff0c\u5728\u6210\u529f+1 \u7684\u65f6\u5019\u9000\u51fa\u5faa\u73af\uff0c\u5426\u5219\u7ee7\u7eed\uff0c \u6bd4\u5982 row = BookInfo.objects.filter(id=book.id, read=origin_read).update(read=origin_read + 1)\uff0c\u5f53 row \u4e3a 0 \u65f6\u7ee7\u7eed\u5faa\u73af\u3002<\/p>\n<p>\u4f46\u662f\u7ecf\u8fc7\u6d4b\u8bd5\uff0c\u4ecd\u7136\u8fbe\u4e0d\u5230 F \u8868\u8fbe\u5f0f\u7684\u6548\u679c\uff0c1000 \u6b21\u8bf7\u6c42\u53ea\u80fd\u52a0\u5230 990 \u5de6\u53f3\uff0c\u6240\u4ee5\uff0c\u8fd8\u662f\u7528 F \u8868\u8fbe\u5f0f\u5427                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157989\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : todd7zhang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @IurNusRay \u6211\u8bd5\u8fc7\uff0cdjango3.1, \u9ed8\u8ba4\u4f7f\u7528 sqlite3 \u6570\u636e\u5e93\uff0c\u4e0d\u5f00\u542f\u4e8b\u52a1\u7684\u65f6\u5019\u30021000 \u6b21\uff0c20 \u5e76\u53d1 sleep \u91cd\u8bd5\u662f\u80fd\u5b9e\u73b0\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157990\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : todd7zhang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/paste.ubuntu.com\/p\/hJVKCFmKtq\/                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157991\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : todd7zhang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u5728\u5916\u9762\u5305\u4e00\u4e2a atomic, \u5728\u6267\u884c filter().update \u65f6\uff0c\u4f1a\u89e6\u53d1 sqlite3.OperationalError: database is locked \u3002\u7136\u540e\u5c1d\u8bd5\u5bf9 save_point rollback \u65f6\u53c8\u6709\u65b0\u7684 An error occurred in the current transaction. You can&#8217;t execute queries until the end of the &#8216;atomic&#8217; block \u3002\u4e0d\u77e5\u9053\u662f\u4e0d\u662f sqlite \u7684 isolation level \u5bfc\u81f4\u7684\uff0c\u6211\u540e\u9762\u8bd5\u4e00\u4e0b postgreSQL \u3002 \u6309\u7406\u6765\u8bf4\uff0c\u53ea\u8981\u662f read committed \u5c31\u53ef\u4ee5\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157992\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : todd7zhang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b9e\u6d4b postgreSQL \u6ca1\u6bdb\u75c5, 1000 \u4e2a\u8bf7\u6c42\uff0c20 \u5e76\u53d1\u3002https:\/\/paste.ubuntu.com\/p\/pxdPM9B8Cx\/<\/p>\n<p>\u6240\u6709\u7684\u670d\u52a1\u90fd\u662f\u76f4\u63a5 runserver                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157993\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nonduality <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @todd7zhang \u6211\u7528 F \u8868\u8fbe\u5f0f\uff0c\u4e0d\u7528 transaction\uff0c1000 \u8bf7\u6c42\uff0c100 \u5e76\u53d1\uff08\u5c1d\u8bd5\u8fc7\u66f4\u9ad8\uff0c\u4f46\u53d7\u7cfb\u7edf\u9650\u5236\u5f00\u4e0d\u8d77\u6765\uff09\uff0c\u591a\u6b21\u6d4b\u8bd5\u90fd\u5b8c\u5168\u6b63\u5e38\u3002\u5982\u679c\u4e0d\u6d89\u53ca\u5173\u952e\u7684\u6570\u503c\uff0c\u7528 F \u8868\u8fbe\u5f0f\u8db3\u591f\u4e86\uff0c\u7528 transaction \u964d\u4f4e\u6570\u636e\u5e93\u6027\u80fd\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4157994\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : todd7zhang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nonduality \u8fd9\u79cd filter().update() \u4e3b\u8981\u8fd8\u662f\u4e3a\u4e86\u9632\u6b62\u8d85\u552e\u5427\uff0c\u5982\u679c\u4f60\u5355\u7eaf\u7684\u4e3a\u4e86+1, F \u8868\u8fbe\u5f0f\u786e\u5b9e\u53ef\u4ee5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e Django \u7684\u4e50\u89c2\u9501\u95ee\u9898 &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\/192558"}],"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=192558"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/192558\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=192558"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=192558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}