Discuss / Python / 运行报错,小白请指教

运行报错,小白请指教

Topic source

运行廖大github上下的代码,运行TEST.py,结果出现:creat_loop()missing 1 required positional argument:‘loop’。本人小白一个,搞不清楚是那里错了。请高手指导一下。

好吧,后来加上LOOP代码如下: import orm,asyncio from models import User,Blog,Comment

def test(loop): yield from orm.create_pool(loop=loop,user='www-data', password='www-data', database='awesome') User(name='Test', email='test@example.com', passwd='1234567890', image='about:blank') yield from u.save()

loop = asyncio.get_event_loop() loop.run_until_complete(test(loop)) loop.close()

结果又报错:TypeError: cannot 'yield from' a coroutine object in a non-coroutine generator。

求大神指导。

好吧,继续更新,替换了@asyncio.coroutine后,目前显示:2003, "Can't connect to MySQL server on 'localhost'"。之前有为兄台@京城派也到这步了,不知后面如何解决的?

恩,很好,在使用mysql -u root -p < schema.sql后,终于也掉进Event loop is closedde的坑里了。 以下是schema.sql的命令:

drop database if exists awesome;

create database awesome;

use awesome;

grant select, insert, update, delete on awesome.* to 'www-data'@'localhost' identified by 'www-data';

create table users ( id varchar(50) not null, email varchar(50) not null, passwd varchar(50) not null, admin bool not null, name varchar(50) not null, image varchar(500) not null, created_at real not null, unique key idx_email (email), key idx_created_at (created_at), primary key (id) ) engine=innodb default charset=utf8;

create table blogs ( id varchar(50) not null, user_id varchar(50) not null, user_name varchar(50) not null, user_image varchar(500) not null, name varchar(50) not null, summary varchar(200) not null, content mediumtext not null, created_at real not null, key idx_created_at (created_at), primary key (id) ) engine=innodb default charset=utf8;

create table comments ( id varchar(50) not null, blog_id varchar(50) not null, user_id varchar(50) not null, user_name varchar(50) not null, user_image varchar(500) not null, content mediumtext not null, created_at real not null, key idx_created_at (created_at), primary key (id) ) engine=innodb default charset=utf8;

Event loop is closedde的坑参照上面几位朋友的说法,销毁LOOP池就可以了。 然后又跌进了“mysql 1062 duplicate entry test@example.com for key idx_email”的坑里。这个原因是因为之前已经在数据库中USER表里面导入数据了。 找到USER表,删除那行数据,终于通过了。

苍天啊,终于爬出这个坑了,我要继续往前爬了。

PS,虽然最近忙的没时间学习,不过数据重复那个坑还是吧我崴得不轻,卡了好几天,终于搞定了,特此发发牢骚。

Name勇哥

#6 Created at ... [Delete] [Delete and Lock User]

感谢提醒,如下代码也可运行:

import orm,sys from models import User, Blog, Comment import asyncio

@asyncio.coroutine def destory_pool():

#global __pool
if orm.__pool is not None :
    orm.__pool.close()
    yield from orm.__pool.wait_closed()

@asyncio.coroutine
def test(): yield from orm.create_pool(user='www-data', password='www-data', db='awesome',loop=loop,host='localhost', port=3306,)

#u = User(name='Test', email='test@example.com', passwd='1234567890', image='about:blank') #重复执行会报错,因为email 有建立unique Key 约束

#yield from u.save()

yield from User(id='0014881168199670ae1cd311c2c4aad9b31dbb9bcb312ae000').remove()
r = yield from User.findAll() 
print(r)
yield from destory_pool()

#创建异步事件的句柄 loop = asyncio.get_event_loop()

loop.run_until_complete(test())

loop.close()

if loop.is_closed(): sys.exit(0)

灵修_星诞

#7 Created at ... [Delete] [Delete and Lock User]

2003, "Can't connect to MySQL server on 'localhost'"。 这个问题你是怎么解决的啊?

至未Shine

#8 Created at ... [Delete] [Delete and Lock User]

TypeError: cannot 'yield from' a coroutine object in a non-coroutine generator。 这个Bug你是怎么解决的,求指点

email 创建的 sql 语句是要求是唯一值,如果插入过一个数据了,改一下第二次保存的 email 就可以了;如果是直接使用老师的代码,遇到kw['db]错误,database 改成 db 就可以了;2003, "Can't connect to MySQL server on 'localhost'"。检查本地 mysql 服务是否已经启动;其他异步错误 自行参考 http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00144661533005329786387b5684be385062a121e834ac7000


  • 1

Reply