Python-如何使用asyncio将基于sqlite3的模块转换为非阻塞模式

时间:2019-06-08 23:55:32

标签: sqlite async-await

我正在将自定义库转换为asyncio模式。

当我尝试获得诅咒时,它会返回协程。 如何将协程转换为光标对象?

这是我要翻译为asyncio的一些代码。

# -*- coding: utf-8 -*-
#  sqlite01.py
import os
import asyncio
import sqlite3
import time
class SQLite():

    def __init__(self,DB='./static/Clientes.db'):
        print("sqlite01>Current Directory:%s" %os.getcwd())
        self.DB=DB

    def GetCursor(self):
         with sqlite3.connect(self.DB) as db:
             return db.cursor()

    async def GetCursor_Async(self):
         async with sqlite3.connect(self.DB) as db:
             return db.cursor()

    def CreateHTML(self,CURSOR,OPT=None):
        para =[]
        for row in CURSOR.execute('''SELECT * FROM cliente ORDER BY nome'''):
            para.append( "<option value=\""+str(row[0])+"\">"+str(row[0])+"</option>")
        return para

我如何测试此模块


import sqlite01
SQL = sqlite01.SQLite()

#Blocking mode
Cursor = SQL.GetCursor()
Result = SQL.CreateHTML(Cursor)
type(Result)
<class 'list'>

#NON-BLOCKING ERROR
CURSOR = SQL.GetCursor_Async()
SQL.CreateHTML(CURSOR)
>>> 

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/felipe/workspace/nwi_prtg/codes/sqlite01.py", line 23, in CreateHTML
    for row in CURSOR.execute('''SELECT * FROM cliente ORDER BY nome'''):
AttributeError: 'coroutine' object has no attribute 'execute'

0 个答案:

没有答案