想在python3.5实现chain coroutine
#!/usr/bin/env python3.5
import sqlite3
import myslice
import json
import asyncio
conn = sqlite3.connect('db.sqlite')
cursor = conn.cursor()
def user():
cursor.execute("SELECT user_id, config, password from user")
for row in cursor:
item = yield row[0], json.loads(row[1]), row[2]
@asyncio.coroutine
def account():
item = yield from user()
user_id = item[0]
# print(user_id)
# c = yield cursor.execute("SELECT config from account WHERE user_id=%s" %(user_id,))
# print(c)
def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(account())
if __name__ == '__main__':
main()
但是错误是RuntimeError: Task got bad yield:, 不知道怎么解决
The official chain coroutine example is like this
async equivalent to @acyncio.coroutine
Obviously the author's method cannot achieve chain coroutine, because the cofunction has to complete the work and cannot have a yield
But it can be done like this