{"id":554532,"date":"2022-02-11T11:34:21","date_gmt":"2022-02-11T03:34:21","guid":{"rendered":"http:\/\/4563.org\/?p=554532"},"modified":"2022-02-11T11:34:21","modified_gmt":"2022-02-11T03:34:21","slug":"%e5%85%b3%e4%ba%8e-asyncio-%e6%89%a7%e8%a1%8c-io-%e5%af%86%e9%9b%86%e5%9e%8b%e6%93%8d%e4%bd%9c%e7%9a%84%e4%b8%8d%e8%a7%a3-7","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=554532","title":{"rendered":"\u5173\u4e8e asyncio \u6267\u884c IO \u5bc6\u96c6\u578b\u64cd\u4f5c\u7684\u4e0d\u89e3"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1> \u5173\u4e8e asyncio \u6267\u884c IO \u5bc6\u96c6\u578b\u64cd\u4f5c\u7684\u4e0d\u89e3 <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <span><i><\/i> 23<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6709\u4e00\u4e2a\u8bfb\u6587\u4ef6\u7136\u540e\u5199\u6570\u636e\u5e93\u7684\u64cd\u4f5c\uff0c\u60f3\u5c1d\u8bd5\u4f7f\u7528\u534f\u7a0b\u3002<br \/> \u4f7f\u7528\u534f\u7a0b\u7684\uff1a <\/p>\n<pre><code>async def parse_text(file_path: Path, context_qs: [asyncio.Queue]):     ql = len(context_qs)     i = 0     # \u6bcf\u4e00\u4e2a Queue \u653e step \u4e2a\u6570\u636e\u5c31\u5207\u6362\u4e0b\u4e00\u4e2a     step = 2     with open(file_path, encoding=\"utf8\") as f:         for text in f:             if i \/\/ step == ql:                 i = 0             context_q = context_qs[i \/\/ step]             context = {}             text = re.findall(r\"d+\", text)             if text:                 context = {\"\u89e3\u6790\u7136\u540e\u7ec4\u88c5\u6210 dict\"}                 await context_q.put(context)                 # \u8fd9\u91cc\u5982\u679c\u4e0d join \uff0c\u4f1a\u4e00\u76f4\u5728\u8fd9\u4e2a for \u5faa\u73af\u91cc\u4e0d\u51fa\u53bb                 await context_q.join()                 i = i + 1         else:             await context_q.put(\"\u7ed3\u675f\u6807\u8bb0\")             return   async def write_db(context_q: asyncio.Queue, model: ModelBase):     async with AsyncSession() as session:         while 1:             context = await context_q.get()             if context[\"\u7ed3\u675f\u6807\u8bb0\"] == \"end\":                 return             info, obj = None, None             try:                 if context[\"info\"]:                     info = await session.execute(                         select(InfoModel).filter(                             InfoModel.attr == context[\"info\"]                         )                     )                     info = info.scalars().one_or_none()                     if not info:                         info = InfoModel(attr=context[\"info\"])                         session.add(info)                 if context[\"header\"]:                     obj = await session.execute(                         select(model).filter(                             model.header == context[\"header\"]                         ).options(selectinload(getattr(model, \"info\")))                     )                     obj = obj.scalars().one_or_none()                     if not obj:                         obj = model(header=context[\"header\"])                         session.add(obj)                 if obj or info:                     if info not in obj.info:                         obj.info.append(info)                         session.add(obj)                     await session.commit()             except Exception as e:                 await session.rollback()                 raise e             else:                 context_q.task_done()   async def main():  # \u6bcf\u4e2a\u8bfb\u53d6\u6587\u4ef6\u5e76\u89e3\u6790\u7684\u65b9\u6cd5\u5bf9\u5e94 c_q_count \u4e2a\u5199\u6570\u636e\u5e93\u7684\u65b9\u6cd5     c_q_count = 3     a_context_qs = [asyncio.Queue() for i in range(c_q_count)]     b_context_qs = [asyncio.Queue() for i in range(c_q_count)]     tasks = [         asyncio.create_task(             parse_text(Path(\"a.txt\"), a_context_qs)         ),         asyncio.create_task(             parse_text(Path(\"b.txt\"), b_context_qs)         ),     ]     for i in range(c_q_count):         tasks.append(asyncio.create_task(write_db(a_context_qs[i], AModel)))         tasks.append(asyncio.create_task(write_db(b_context_qs[i], BModel)))     await asyncio.gather(*tasks)    if __name__ == '__main__':     asyncio.run(main(), debug=settings.DEBUG)  <\/code><\/pre>\n<p>\u4e0d\u4f7f\u7528\u534f\u7a0b\u7684\uff1a <\/p>\n<pre><code>def sync_read_file():     af = Path(\"a.txt\").open(encoding=\"utf8\")     bf = Path(\"b.txt\").open(encoding=\"utf8\")     with Session() as session:         while 1:             if af:                 try:                     text = af.readline()                     context = parse_text(text)                     sync_write_db(session, context, AModel)                 except IOError:                     af.close()                     af = None             if bf:                 try:                     text = bf.readline()                     context = parse_text(text)                     sync_write_db(session, context, BModel)                 except IOError:                     bf.close()                     bf = None             if not af and not bf:                 return   def sync_write_db(session, context, model):     info, obj = None, None     try:         if context[\"info\"]:             info = session.execute(                 select(Info).filter(                     Info.attr == context[\"info\"]                 )             )             info = info.scalars().one_or_none()             if not info:                 info = Info(attr=context[\"info\"])                 session.add(info)         if context[\"header\"]:             obj = session.execute(                 select(model).filter(model.info == context[\"info\"]))             obj = obj.scalars().one_or_none()             if not obj:                 obj = model(info=context[\"info\"])                 session.add(obj)         if obj or info:             if info not in obj.info:                 obj.info.append(info)                 session.add(obj)             session.commit()     except Exception as e:         session.rollback()         raise e   if __name__ == '__main__':     sync_read_file()  <\/code><\/pre>\n<p>\u8fd9\u4e2a\u534f\u7a0b\u7684\u65b9\u6cd5\uff0c\u6bcf\u79d2\u6bcf\u4e2a\u8868\u53ef\u4ee5\u5199 400 \u591a\u884c\uff0c\u6539\u4e3a\u540c\u6b65\u5355\u7ebf\u7a0b\u7684\u8fd8\u662f\u6bcf\u79d2\u5199 400 \u591a\u884c\u3002<br \/> \u4e0d\u77e5\u9053\u662f\u6211\u534f\u7a0b\u7684\u7528\u6cd5\u6709\u95ee\u9898\uff1f\u8fd8\u662f\u8bf4\u6709\u522b\u7684\u4ec0\u4e48\u539f\u56e0\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>35<\/span>) <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"7111863\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : long2ice <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u8bd5\u8bd5 aiofiles \u4e4b\u7c7b\u7684\uff0c\u4f60\u7684\u6587\u4ef6 IO \u8fd8\u662f\u540c\u6b65\u7684 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111864\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Trim21 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> with open(file_path, encoding=&#8221;utf8&#8243;) as f:<br \/> for text in f:<br \/>\u8fd9\u4e24\u884c\u90fd\u662f\u963b\u585e\u7684 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111865\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @long2ice #1 <br \/>@Trim21 #2 <br \/>\u6587\u4ef6\u8fd9\u91cc\u53ea\u662f\u8bfb\u53d6\uff0c\u7136\u540e\u653e\u8fdb\u961f\u5217\u91cc\uff0c\u8fd9\u4e5f\u4f1a\u5bfc\u81f4\u963b\u585e\u5417\uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111866\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Trim21 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @firejoke #3 \u540c\u6b65 io \u4f1a\u963b\u585e\u6389\u6574\u4e2a\u4e8b\u4ef6\u5faa\u73af\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111867\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @Trim21 #4 \u6240\u4ee5\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u5982\u679c\u4e0d\u4e3b\u52a8\u7528\u961f\u5217\u7684 join \u963b\u585e\u4f4f\uff0c\u5c31\u4e0d\u4f1a\u8df3\u5230\u5176\u4ed6 await \u7684\u5730\u65b9\uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111868\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Nitroethane <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @firejoke \u5982\u679c\u8bfb\u53d6\u6587\u4ef6\u901f\u5ea6\u6bd4\u8f83\u6162\uff0c\u800c\u4e14\u6587\u4ef6\u6bd4\u8f83\u5927\u7684\u8bdd\u5f71\u54cd\u5e94\u8be5\u6bd4\u8f83\u660e\u663e <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111869\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @Nitroethane #6 \u6bcf\u884c\u6570\u636e\u5c0f\u4e8e 1kb \uff0c\u800c\u4e14\u662f\u7528\u7684 for \uff0c\u8fd9\u91cc\u76f8\u5f53\u4e8e\u4e00\u4e2a\u751f\u6210\u5668  <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111870\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Trim21 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @firejoke #5 \u4e0d\u662f\uff0c\u4f60\u7684\u4ee3\u7801\u4e2d\u4ec5\u4ec5\u4f1a\u963b\u585e\u5728 open \u548c for text in f \u8fd9\u4e24\u884c\u3002\u5728\u7b49\u5f85\u8fd9\u4e24\u884c\u5e95\u5c42\u7684\u540c\u6b65 io \u5b8c\u6210\u7684\u65f6\u95f4\u91cc\u662f\u4e0d\u4f1a\u8fd0\u884c\u5176\u4ed6 task \u7684\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111871\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @Trim21 #8 \u6211\u6539\u6210\u4e86 asyncfiles \uff0c\u7136\u540e\u628a\u961f\u5217\u7684 join \u53bb\u6389\u4e86\uff0c\u8fd9\u6b21\u6210\u529f\u8df3\u5230\u4e86\u5176\u4ed6 await \u7684\u4f4d\u7f6e\uff0c\u786e\u5b9e\u5982\u4f60\u6240\u8bf4\uff0c\u611f\u8c22\uff01<br \/>\u4f46\u6d4b\u8bd5\u53d1\u73b0\uff0c\u867d\u7136\u6ca1\u4e86 io \u7684\u963b\u585e\uff0c\u4f46\u5199\u5165\u901f\u5ea6\u8fd8\u662f\u6ca1\u592a\u5927\u53d8\u5316\uff0c\u4ed6\u6bcf\u8bfb\u4e00\u884c\uff0c\u5207\u5230\u5176\u4ed6 task \uff0c\u548c\u6211\u4e4b\u524d\u6ca1\u8bfb\u4e00\u884c\uff0cjoin \u4f4f\uff0c\u5c31\u6267\u884c\u6d41\u7a0b\u6765\u8bf4\uff0c\u662f\u4e0d\u662f\u6ca1\u5dee\uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111872\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Trim21 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u6211\u6ca1\u4ed4\u7ec6\u770b\u5b8c\u6574\u7684\u4ee3\u7801\uff0c\u53ea\u662f\u770b\u5230\u4e00\u5f00\u59cb\u5c31\u6709\u540c\u6b65\u963b\u585e\u7684\u95ee\u9898\u5c31\u56de\u590d\u4e86\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111873\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : locoz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u76ee\u6d4b\u662f\u6b63\u5219\u5bfc\u81f4\u7684\u963b\u585e&#8230;\u6709\u4e00\u8bf4\u4e00\u4f60\u8fd9\u79cd\u60c5\u51b5\u4e0d\u592a\u9002\u5408\u7528 asyncio \uff0c\u6216\u8005\u8bf4\u4e0d\u592a\u9002\u5408\u6ca1\u6709\u5305\u4e0a\u9690\u5f0f\u591a\u8fdb\u7a0b\u7684 asyncio \uff0c\u6bd5\u7adf\u4e0d\u662f\u7eaf\u7cb9\u7684 IO \u64cd\u4f5c\u3002\u7136\u540e\u6587\u4ef6\u64cd\u4f5c\u65b9\u9762 aiofiles \u5b9e\u9645\u80cc\u540e\u4e5f\u662f\u9760\u7ebf\u7a0b\u6c60\u8dd1\u7684\uff0c\u8fd9\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u4e00\u4e0b\uff0c\u6709\u65f6\u5019\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8e29\u5751\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111874\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : documentzhangx66 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u5148\u76d1\u89c6\u4e00\u4e0b\u8bbe\u5907\u6027\u80fd\u6781\u9650\u3002<br \/>iostat -x -m -d 1 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111875\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : LeeReamond <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u5927\u6982\u770b\u4e86\u4e00\u773c\u4e0a\u8bf4\u7684\u5e94\u8be5\u6ca1\u95ee\u9898\uff0c\u5e76\u975e\u6240\u6709\u7c7b\u578b\u7684\u4efb\u52a1\u90fd\u80fd\u901a\u8fc7\u5f02\u6b65\u52a0\u901f\uff0c\u4f60\u8981\u505a\u597d\u5fc3\u7406\u51c6\u5907\u3002\u53e6\u5916 aiofiles \u7684\u5b9e\u73b0\u5176\u5b9e\u5f88\u4e11\u964b\u3002\u3002\u4e0a\u8bf4\u662f\u7ebf\u7a0b\u6c60\u8dd1\u7684\uff0c\u6211\u6709\u70b9\u5fd8\u8bb0\u5177\u4f53\u60c5\u51b5\u4e86\uff0c\u53ea\u8bb0\u5f97\u4ee5\u524d\u8bfb\u6e90\u7801\u7684\u5370\u8c61\u662f\u5f88\u4e11\u964b\u3002\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111876\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Contextualist <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u770b\u4e0a\u53bb\u6ca1\u6709\u660e\u663e\u7684\u95ee\u9898\uff0c\u4e0d\u8fc7\u5bf9\u4e8e\u4efb\u4f55\u4e3a\u4e86\u6539\u8fdb\u6027\u80fd\u7684\u91cd\u5199\u5efa\u8bae\u8fd8\u662f\u5148 profile \u4e00\u4e0b\uff0c\u770b\u770b\u74f6\u9888\u5230\u5e95\u51fa\u5728\u54ea\u4e2a\u8c03\u7528\u4e0a\u3002<br \/>\u7136\u540e\u5f02\u6b65\u6587\u4ef6 IO \u4e0d\u662f\u4e3a\u4e86\u63d0\u5347\u6027\u80fd\uff08\u964d\u4f4e\u5e73\u5747\u5ef6\u8fdf\uff09\u7684\uff0c\u800c\u662f\u4e3a\u4e86\u964d\u4f4e\u5c3e\u5ef6\u8fdf\u7684\uff0c\u53c2\u89c1\uff1a https:\/\/trio.readthedocs.io\/en\/stable\/reference-io.html#background-why-is-async-file-i-o-useful-the-answer-may-surprise-you <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111877\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 2i2Re2PLMaDnghL <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \uff08\u6211\u4f1a\u5c1d\u8bd5\u5148\u628a\u6240\u6709\u4fe1\u606f\u8bfb\u8fdb\u5185\u5b58\u7136\u540e timeit \u6570\u636e\u5e93\u90e8\u5206\uff0c\u770b\u74f6\u9888\u662f\u4e0d\u662f\u6587\u4ef6  <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111878\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lesismal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u4e0d\u662f\u8bf4\u7ed9\u51fd\u6570\u52a0\u4e0a\u5f02\u6b65\u5c31\u662f\u4e00\u5207\u90fd\u5f02\u6b65\u4e86\uff1a<br \/>1. \u5f02\u6b65\u7684\u51fd\u6570 A<br \/>2. A \u5185\u90e8\u8c03\u7528 B C D \uff0cB C D \u6709\u4efb\u610f\u540c\u6b65\u963b\u585e\u7684\u884c\u4e3a\uff0cA \u4e5f\u4e00\u6837\u8ddf\u7740\u963b\u585e<br \/>py \u7684\u6027\u80fd\u75db\u70b9\u8fdc\u4e0d\u53ea\u662f asyncio \u5c31\u80fd\u89e3\u51b3\u7684\u4e86\u7684\uff0chow about trying golang -_- <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111879\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @locoz #11 \u6211\u4e5f\u611f\u89c9\u4f3c\u4e4e\u6ca1\u53d1\u6325\u51fa asyncio \u7684\u4f18\u52bf\uff0c\u6bcf\u4e00\u6761\u6570\u636e\u90fd\u4e0d\u8d85\u8fc7 1kb \uff0c\u6240\u4ee5\u53ef\u80fd\u9664\u4e86\u6570\u636e\u5e93\u64cd\u4f5c\u7a0d\u5fae\u8017\u65f6\u957f\u4e00\u70b9\uff0c\u5176\u4ed6\u5730\u65b9\u7b49\u5f85\u7684\u5f88\u5c11\uff0c\u6240\u4ee5\u548c\u5355\u7ebf\u7a0b\u7684\u6027\u80fd\u5dee\u4e0d\u591a\uff1f\u53e6\u5916\u8bf7\u6559\u4e00\u4e0b\uff0c\u201c\u6ca1\u6709\u5305\u4e0a\u9690\u5f0f\u591a\u8fdb\u7a0b\u201d \u5177\u4f53\u662f\u6307\u4ec0\u4e48\u5462\uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111880\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @documentzhangx66 #12 \u8bbe\u5907\u6027\u80fd\u5e94\u8be5\u6ca1\u95ee\u9898\uff0c12 \u6838 24 \u7ebf\u7a0b\uff0c64G \u5185\u5b58\uff0c\u78c1\u76d8\u8bfb\u53d6\u901f\u5ea6\u4e5f\u6ca1\u6709\u8dd1\u6ee1\uff0cIO \u8bfb\u5199\u4e5f\u4e0d\u662f\u7279\u522b\u9ad8\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111881\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @LeeReamond #13 \u55ef\uff0c\u6211\u6628\u5929\u4e5f\u60f3\u4e86\u4e00\u4e0b\uff0c\u5982\u679c\u6bcf\u4e00\u6b65\u963b\u585e\u4f4f\u7684\u64cd\u4f5c\u5b9e\u9645\u4e0a\u90fd\u5f88\u5feb\uff0c\u90a3 asyncio \u5176\u5b9e\u53d1\u6325\u4e0d\u51fa\u5207\u6362\u7b49\u5f85\u7684\u4f18\u52bf\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111882\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : locoz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @firejoke #17 \u5efa\u8bae\u7528\u8c03\u8bd5\u5de5\u5177\u6216\u8005\u6392\u9664\u6cd5\u770b\u770b\u5177\u4f53\u662f\u54ea\u91cc\u62d6\u6162\u4e86\uff0c\u5355\u770b\u4ee3\u7801\u548c\u524d\u9762\u7684\u8ba8\u8bba\u6211\u611f\u89c9\u662f\u6b63\u5219\u90e8\u5206\u5bfc\u81f4\u7684\u3002<br \/>\u524d\u9762\u6ca1\u8bb2\u6e05\u695a\uff0c\u201c\u5305\u4e0a\u9690\u5f0f\u591a\u8fdb\u7a0b\u7684 asyncio\u201d\u6307\u7684\u662f\u628a\u591a\u8fdb\u7a0b\u548c\u534f\u7a0b\u7ed3\u5408\uff0c\u5f00\u4e00\u5806\u5b50\u8fdb\u7a0b\u7136\u540e\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u4e00\u4e2a eventloop \uff0c\u56e0\u4e3a\u4e4b\u524d\u6709\u770b\u5230\u8fc7\u4e00\u4e2a\u4e13\u95e8\u7684\u5e93\u628a\u8fd9\u90e8\u5206\u64cd\u4f5c\u7ed9\u9690\u5f0f\u5904\u7406\u4e86\uff0c\u4f7f\u7528\u8d77\u6765\u4e24\u4e09\u884c\u641e\u5b9a\uff0c\u4e0d\u9700\u8981\u81ea\u5df1\u5199\u8fdb\u7a0b\u7ba1\u7406\u90e8\u5206\u3002\u7136\u540e\u4e00\u4e9b\u6846\u67b6\u5176\u5b9e\u4e5f\u4f1a\u9690\u5f0f\u5730\u505a\u8fd9\u79cd\u7ed3\u5408\u5904\u7406\u6765\u63d0\u9ad8\u6548\u7387\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111883\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @Contextualist #14 \u770b\u6587\u6863\u7684\u610f\u601d\uff0c\u662f\u8bf4\u7528\u5f02\u6b65\u6587\u4ef6 IO \uff0c\u5728\u4ece\u5185\u5b58\u8bfb\u53d6\u65f6\u53cd\u5012\u4f1a\u53d8\u6162\uff0c\u5728\u4ece\u78c1\u76d8\u8bfb\u53d6\u7684\u65f6\u5019\u4f1a\u52a0\u5feb\uff0c\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u5176\u7ed3\u679c\u662f\u4e0d\u53ef\u9884\u6d4b\u7684\u3002\u90a3\u6211\u5982\u679c\u5355\u72ec\u7528\u4e00\u4e2a\u8fdb\u7a0b\u8bfb\u53d6\u6587\u4ef6\u5230\u5185\u5b58\uff0c\u7136\u540e\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u4ece\u5185\u5b58\u8bfb\u53d6\u7136\u540e\u518d\u64cd\u4f5c\uff0c\u5e94\u8be5\u53ef\u4ee5\u7ed5\u5f00\u8fd9\u4e2a\u95ee\u9898\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111884\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @locoz #20 \u6211\u6628\u5929\u6700\u540e\u4e5f\u662f\u6539\u6210\u7528\u591a\u8fdb\u7a0b\u4e86\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u4e13\u95e8\u8bfb\u6587\u4ef6\uff0c\u7136\u540e\u653e\u8fdb\u961f\u5217\uff0c\u5176\u4ed6\u5b50\u8fdb\u7a0b\u4ece\u961f\u5217\u8bfb\uff0c\u7136\u540e\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u90a3\u770b\u6765\u6211\u601d\u8def\u6ca1\u8dd1\u504f\u3002\u8fd8\u6709\u5176\u4ed6\u7684\u89e3\u6cd5\u5417\uff1f\u591a\u8fdb\u7a0b\u548c\u534f\u7a0b\u7684\u7ed3\u5408\uff0c\u4e00\u822c\u90fd\u662f\u4ee5\u591a\u8fdb\u7a0b\u4e3a\u4e3b\u5417\uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111885\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Contextualist <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u53c8\u770b\u4e86\u4e00\u4e0b\u4f60\u8d34\u51fa\u6765\u6587\u4ef6\u7684\u90e8\u5206\uff0c\u4f60\u662f\u4e0d\u662f\u5c31\u4e24\u4e2a\u5927\u6587\u4ef6\uff08\u5c31\u662f\u8bf4\u4e0d\u662f\u5927\u91cf\u5c0f\u6587\u4ef6\uff09\uff0c\u90a3\u6587\u4ef6 IO \u5c31\u57fa\u672c\u4e0d\u53ef\u80fd\u662f\u4f60\u7684\u74f6\u9888\uff0c\u4f60\u770b\u5230\u78c1\u76d8\u8bfb\u53d6\u6ca1\u8dd1\u6ee1\u5f88\u6709\u53ef\u80fd\u662f\u4f60\u4e0b\u6e38\u7684\u5904\u7406\u901f\u5ea6\u6ca1\u8ddf\u4e0a\u3002<br \/>\u591a\u8fdb\u7a0b\u548c\u534f\u7a0b\uff0c\u611f\u89c9\u4f60\u81ea\u5df1\u4e5f\u603b\u7ed3\u51fa\u6765\u4e86\u3002\u534f\u7a0b\u5f97\u7528\u5728\u6709\u957f\u65f6\u95f4\u7b49\u5f85\u7cfb\u7edf\u8c03\u7528 (syscall) \u7684\u5730\u65b9\uff08\u6bd4\u5982\u7f51\u7edc\u3001\u5b50\u8fdb\u7a0b\u3001\u5b9a\u65f6\u4efb\u52a1\uff09\u3002CPU \u5bc6\u96c6\u7684\u64cd\u4f5c\u5f97\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\uff0c\u4f46\u5728 Python \u91cc\u6709 GIL \uff0c\u5c31\u53ea\u80fd\u7528\u591a\u8fdb\u7a0b\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111886\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @Contextualist #23 \u662f\u7684\uff0c\u5c31\u662f\u4e24\u4e2a\u5927\u6587\u4ef6\uff0c\u6240\u4ee5\u6211\u4e5f\u89c9\u5f97\u6587\u4ef6 IO \u4e0d\u662f\u6211\u8fd9\u91cc\u7684\u74f6\u9888\uff0c\u534f\u7a0b\u5728\u8fd9\u4e2a\u573a\u666f\u4e2d\u6ca1\u4f53\u73b0\u51fa\u4ed6\u7684\u4f18\u52bf\uff0c\u6211\u5df2\u7ecf\u6539\u6210\u4e86\u591a\u8fdb\u7a0b\u4e86\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111887\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Contextualist <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u6211\u5bf9\u6570\u636e\u5e93\u4e0d\u719f\uff0c\u4e0d\u8fc7\u6211\u731c\u5bf9\u4e8e\u5f88\u591a\u6570\u636e\u5e93\u5e76\u53d1\u5199\u662f\u4e0d\u4f1a\u6709\u6027\u80fd\u63d0\u5347\u7684\uff0c\u7528\u5355\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u4e86\uff0c\u4f46\u4f60\u53ef\u80fd\u9700\u8981 batch \/ bulk \u64cd\u4f5c\uff0c\u7528\u6765\u4e00\u6b21\u6027\u63d2\u5165\u6570\u5341\u6761\u3001\u6570\u767e\u6761\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u63d2\u5165\u4e00\u6761\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111888\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : O5oz6z3 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u867d\u7136\u4e0d\u61c2\uff0c\u770b\u5b8c\u4e0a\u611f\u89c9\u539f\u56e0\u4e4b\u4e00\u5728\u4e8e asyncio \u7684\u4e0a\u9650\u5c31\u662f\u5355\u7ebf\u7a0b\uff0c\u800c\u5355\u7ebf\u7a0b\u541e\u5410\u91cf\u4e0d\u5982\u591a\u7ebf\u7a0b\uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111889\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @Contextualist #25 \u5bf9\u6b38\uff01\u8d44\u6e90\u662f\u6d88\u8017\u5728\u6bcf\u4e00\u6761\u67e5\u8be2\u548c\u5199\u5165\u7684\u64cd\u4f5c\u4e0a\uff0c\u5982\u679c\u6279\u91cf\u5199\uff0c\u5c31\u53ef\u4ee5\u964d\u4f4e\u5199\u5165\u9891\u7387\uff0c\u81f3\u4e8e\u67e5\u8be2\uff0c\u6211\u5df2\u7ecf\u5728\u67e5\u8be2\u5b57\u6bb5\u4e0a\u52a0\u4e86\u7d22\u5f15\uff0c\u6211\u6539\u4e00\u4e0b\u8bd5\u8bd5\u3002\u611f\u8c22~<br \/>\u7136\u540e\u6211\u770b\u5230\u4f60\u4e4b\u524d\u63d0\u5230\u7684 trio \uff0c\u770b\u4ed6\u7684\u6587\u6863\u50cf\u662f\u6d89\u53ca\u5230\u5f02\u6b65\u64cd\u4f5c\u7684\u90fd\u6709\u6d89\u53ca\uff0c\u611f\u89c9\u975e\u5e38\u4e0d\u9519\u554a\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111890\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @O5oz6z3 #26 \u4e0d\u662f\uff0c\u5f53\u4e0d\u5b58\u5728\u8f83\u957f\u7684 io \u7b49\u5f85\u7684\u65f6\u5019\uff0c\u534f\u7a0b\u548c\u5355\u7ebf\u7a0b\u6ca1\u5dee\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111891\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yufpga <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u5927\u6982\u770b\u4e86\u4e0b, \u8fd9\u74f6\u9888\u663e\u7136\u4e0d\u662f\u5728 parse_text \u4e2d\u7684\u6587\u4ef6\u8bfb,\u5c31\u7b97\u518d\u600e\u4e48\u963b\u585e\uff0c\u8bfb\u5199\u672c\u5730\u6587\u4ef6\u4e5f\u4e0d\u81f3\u4e8e\u5230\u6bcf\u79d2\u624d 400 \u884c\u7684\u7a0b\u5ea6. \u800c\u5728 write_db \u4e2d, \u51fa\u73b0\u597d\u51e0\u5904 await \u7684\u5730\u65b9, \u8fd9\u4e9b\u5730\u65b9\u53ef\u90fd\u662f\u8981\u540c\u6b65\u7b49\u5f85\u7ed3\u679c\u8fd4\u56de\u7684\u5440. \u4e00\u4e2a\u5f88\u597d\u5bb9\u6613\u9a8c\u8bc1\u7684\u65b9\u6cd5\u5c31\u662f\u628a write_db \u4e2d\u7684 await \u7528 await asyncio.sleep \u66ff\u6362\u6389, \u5c1d\u8bd5\u4e0d\u540c\u7684 sleep \u65f6\u95f4. \u5b9e\u9645\u4e0a\u4e0a\u9762\u7684\u95ee\u9898\u5728\u4e8e\u6bcf\u4e00\u6b21 while 1 \u7684\u5faa\u73af\u5faa\u73af\u662f\u540c\u6b65\u7684, \u4f60\u5fc5\u987b\u8981\u5148\u5904\u7406\u5b8c\u961f\u5217\u4e2d\u7684\u524d\u4e00\u6761\u6570\u636e, \u624d\u80fd\u7ee7\u7eed\u5904\u7406\u4e0b\u4e00\u6761\u6570\u636e. \u6240\u4ee5\u5904\u7406\u4e5f\u5f88\u7b80\u5355, \u628a\u6bcf\u4e00\u6b21\u7684\u5faa\u73af\u5f02\u6b65\u5316\u6389. <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111892\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hustlibraco <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u7528&#8220;`async for&#8220;`\u66ff\u4ee3&#8220;`for&#8220;`\u53ef\u4ee5\u5417\uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111893\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @hustlibraco #30 \u6362\u6210\u5f02\u6b65\u6587\u4ef6\u8bfb\uff0c\u5c31\u53ef\u4ee5\u6362\u6210 async for \u4e86\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111894\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @yufpga #29 \u6211\u770b\u65e5\u5fd7\u91cc\uff0c\u6211\u540c\u65f6\u5f00\u4e86\u597d\u591a\u4e2a task \uff0c\u8fd9\u4e2a task \u7684\u5faa\u73af\u91cc await query \u6216 add \u6216 commit \uff0c\u5c31\u4f1a\u8df3\u5230\u53e6\u4e00\u4e2a task \u7684\u5faa\u73af\u91cc\u7684 query \u6216 add \u6216 commit \u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111895\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yufpga <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @firejoke \u662f\u6211\u770b\u5dee\u4e86, \u6211\u4ee5\u4e3a\u53ea\u6709\u4e00\u4e2a queue, \u800c\u4f60\u7684\u4ee3\u7801\u91cc\u662f\u4e24\u4e2a context, \u5404\u81ea 3 \u4e2a queue, \u4e5f\u5c31\u662f\u603b\u5171 6 \u4e2a queue, \u5bf9\u5e94 6 \u4e2a write_db \u7684 task. \u5f53\u9047\u5230 await \u7684\u65f6\u5019, \u786e\u5b9e\u662f\u4f1a\u8df3\u8f6c\u5230\u522b\u7684 task \u91cc\u9762\u6267\u884c. \u786e\u5b9e\u6bd4\u8f83\u5947\u602a\uff0c\u4f46\u6211\u4ecd\u7136\u89c9\u5f97\u74f6\u9888\u4e0d\u5927\u53ef\u80fd\u5728 parse_text, \u4f60\u53ef\u4ee5\u8bd5\u7740\u8bb0\u5f55\u4e00\u4e0b\u961f\u5217\u5199\u5165\u6570\u636e\u7684\u901f\u7387\uff0c \u5982\u679c\u8fd9\u4e2a\u901f\u7387\u4e5f\u5728 400\/s \u5de6\u53f3, \u90a3\u8bf4\u660e\u786e\u5b9e\u6709\u53ef\u80fd\u662f parse_text \u6162\u4e86 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111896\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ohayoo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @firejoke \u8001\u54e5\u53ef\u4ee5\u5206\u4eab\u4e0b\u591a\u8fdb\u7a0b\u7248\u672c\u7684\u4ee3\u7801\u5417\uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7111897\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : firejoke <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @ohayoo #34 \u8fd8\u5728\u8c03\u8bd5\u591a\u8fdb\u7a0b\u548c\u534f\u7a0b\u7684\u7ec4\u5408\uff0c\u540e\u9762\u4f1a\u8d34\u4e00\u4e0b\u7684\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e asyncio \u6267\u884c IO &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/554532"}],"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=554532"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/554532\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=554532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=554532"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=554532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}