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行寫數據庫操作可以,求問一下為什么
順便問一下這樣寫有什么不好?
總結
以上是生活随笔為你收集整理的python 异步执行多个函数_Python的asyncio模块写多个异步的执行顺序和影响?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自动化运维系统具备的功能
- 下一篇: 数据层的多租户浅谈(SAAS多租户数据库