{"id":430888,"date":"2021-04-18T12:00:27","date_gmt":"2021-04-18T04:00:27","guid":{"rendered":"http:\/\/4563.org\/?p=430888"},"modified":"2021-04-18T12:00:27","modified_gmt":"2021-04-18T04:00:27","slug":"%e6%83%b3%e9%97%ae%e4%b8%8b-sqlalchemy1-4-%e7%9a%84%e5%bc%82%e6%ad%a5%e6%a8%a1%e5%bc%8f%e6%9c%89%e4%ba%ba%e8%af%95%e8%bf%87%e4%b9%88","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=430888","title":{"rendered":"\u60f3\u95ee\u4e0b sqlalchemy1.4 \u7684\u5f02\u6b65\u6a21\u5f0f\u6709\u4eba\u8bd5\u8fc7\u4e48"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u60f3\u95ee\u4e0b sqlalchemy1.4 \u7684\u5f02\u6b65\u6a21\u5f0f\u6709\u4eba\u8bd5\u8fc7\u4e48               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zyxbcde <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u8fd9\u91cc\u662f\u4e00\u4e2a\u88ab\u5f00\u53d1\u903c\u7740\u5199\u4ee3\u7801\u7684\u83dc\u9e21\u5546\u52a1\u3002<\/p>\n<p>sqlalchemy \u51fa\u4e86 1.4 \u4ee5\u540e\u60f3\u8bd5\u8bd5\u5f02\u6b65\u6a21\u5f0f\uff0c\u5c31\u627e\u4e86\u4e2a mysql \u6570\u636e\u5e93\u5199\u4e86\u51e0\u884c\u6d4b\u8bd5\u3002<\/p>\n<p>import asyncio<\/p>\n<p>from sqlalchemy.ext.asyncio import create_async_engine<\/p>\n<p>from sqlalchemy.ext.asyncio import AsyncSession<\/p>\n<p>async def main():<\/p>\n<pre><code>engine = create_async_engine(\"mysql+aiomysql:\/\/root:[email&#160;protected]:3333\/test\")  session = AsyncSession(engine)  await session.execute(\"select * from students\")  await session.close() <\/code><\/pre>\n<p>if <strong>name<\/strong> == &#8216;<strong>main<\/strong>&#8216;:<\/p>\n<pre><code>asyncio.run(main()) <\/code><\/pre>\n<p>\u7136\u540e\u5728 aiomysql \u8fde\u63a5\u5173\u95ed\u7684\u65f6\u5019\u62a5\u9519 event loop is closed\uff0c\u6211\u611f\u89c9\u6211\u7684\u4ee3\u7801\u5199\u7684\u6ca1\u95ee\u9898\u554a\uff0c\u5c31\u8fd9\u4e48\u4e09\u56db\u884c\uff0c\u7136\u540e\u6211\u8fd8\u4e0d\u7518\u5fc3\u628a\u5b98\u65b9\u6587\u6863\u91cc\u7684\u4f8b\u5b50\u6539\u6210 aiomysql \u4e22\u8fdb\u53bb\u8fd8\u662f\u4e00\u6837\u7684\u9519\u3002<\/p>\n<p>\u60f3\u8bf7\u8bd5\u8fc7\u7684\u5927\u795e\u7ed9\u6307\u5bfc\u4e0b<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5645037\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Te11UA <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u76ee\u6d4b\u7528\u7684\u662f Windows<br \/>Linux \u73af\u5883\u5e94\u8be5\u4e0d\u4f1a\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5645038\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Wincer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u7528\u8fc7\u5f02\u6b65\u6a21\u5f0f\uff0c\u4f46\u662f\u4f60\u7684\u5199\u6cd5\u4f3c\u4e4e\u4e0e\u5b98\u65b9\u6587\u6863\u7684\u4e0d\u592a\u4e00\u81f4\u3002https:\/\/docs.sqlalchemy.org\/en\/14\/orm\/extensions\/asyncio.html\uff0c\u6587\u6863\u91cc\u662f\u4f7f\u7528 async with async_session() as session: \u8fd9\u6837\u6765\u521b\u9020 session \u7684\uff0c\u4f60\u53ef\u4ee5\u6309\u6587\u6863\u91cc\u8bf4\u7684\u8bd5\u8bd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5645039\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zyxbcde <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Te11UA \u6000\u7591\u8fc7\uff0c\u6211\u751a\u81f3\u628a python \u5220\u4e86\u91cd\u88c5\u4e86\u4e00\u904d\uff0c\u7b49\u56de\u516c\u53f8\u8bd5\u8bd5\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5645040\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zyxbcde <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Wincer \u5f02\u6b65\u6a21\u5f0f\u521a\u51fa\u7684\uff0c\u6211\u4e5f\u662f\u8bd5\u8bd5\uff0c\u4e0d\u8fc7\u8fd9\u91cc\u8ddf with \u6ca1\u5173\u7cfb\uff0c\u7528 with \u4e5f\u662f\u4e00\u6837\u7684\u9519\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u60f3\u95ee\u4e0b sqlalchemy1.4&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\/430888"}],"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=430888"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/430888\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=430888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=430888"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=430888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}