python 异步执行多个函数_Python的asyncio模块写多个异步的执行顺序和影响?

写了小爬虫,用到asyncio做异步,多次调用async with 和 await爬虫操作,但是在其中没调用异步的数据库操作没有成功,代码下:

async with session.get(restaurant_url) as restaurant_rep:

restaurant_rep = await restaurant_rep.json()

rating_url = RES_URL.format(restaurant_id=restaurant_id)

async with session.get(rating_url) as rating_rep:

rating_rep = await rating_rep.json()

overall_rating, service_rating, food_rating = format_rating_data(rating_rep)

add_restaurant_data(restaurant_rep, overall_rating, service_rating, food_rating)

food_url = constants.ELE_FOOD_JSON.format(restaurant_id=restaurant_id)

async with session.get(food_url) as food_rep:

food_rep = await food_rep.json()

for food_type in food_rep:

for food in food_type['foods']:

food_id = food['specfoods'][0]['food_id']

if food_id not in food_set:

food_set.add(food_id)

add_food_data(food)

add_*函数 解析response并写入数据库, 但是8行写操作并没有写成功, 函数没问题, 19行写数据库操作可以,求问一下为什么

顺便问一下这样写有什么不好?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值