V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
DOUWH
V2EX  ›  Python

Python Flask + asyncion 写了一个数据采集的接口,一直报错,求大佬帮忙给治下~~~

  •  
  •   DOUWH · 2019-04-11 18:23:46 +08:00 · 3768 次点击
    这是一个创建于 1839 天前的主题,其中的信息可能已经有所发展或是发生改变。

    先贴出来错误: RuntimeError: There is no current event loop in thread 'Thread-2'. 问题描述: 我使用 asyncio,aiohttp 进行了数据的抓取,想把这一模块封装成一个接口,调用接口的时候就会报错。 本人小白- - 对 flask 理解不深,不知道是接口写的有问题,还是哪里的问题。

    贴出代码: 开启事件循环: def run(self, name, sku, page_start, page_end, sort, price_start, price_end): loop = asyncio.get_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete( self.async_run(start_page, end_page, goods_name, rank_type, price))

    flask 接口:

    -- coding: utf-8 --

    import flask import json import asyncio from flask import Flask, request from jd_global_shopping import jd_qqg

    app = Flask(name) jd = jd_qqg()

    @app.route("/global_shopping", methods=["POST"]) def check(): jd = jd_qqg() params = flask.request.json res = {'return_code': '200', 'return_info': '处理成功', 'result': ''}

    if params:
        name = params.get('name')
        sku = params.get('sku')
        page_start = params.get('page_start')
        page_end = params.get('page_end')
        # type = params.get('type')
        sort = params.get('sort')
        price_start = params.get('price_start', '')
        price_end = params.get('price_end', '')
    
        if name and page_start and page_end and sort:
            if type(name) != str:
                res = {"error_code": 3003, "msg": "输入关键字格式错误"}
            # elif 'item.jd.hk' not in sku or not sku.isdigit():
            #     res = {"error_code": 3004, "msg": "输入链接格式错误"}
            elif not page_start.isdigit():
                res = {"error_code": 3004, "msg": "输入页码格式错误"}
            elif not page_end.isdigit():
                res = {"error_code": 3004, "msg": "输入页码格式错误"}
            elif not sort.isdigit():
                res = {"error_code": 3004, "msg": "输入类型格式错误"}
            else:
                jd.run(name, sku, page_start, page_end, sort, price_start, price_end)
        else:
            res = {"error_code": 3007, "msg": "必填参数未填写"}
    else:
        res = {"error_code":3002,"msg":"入参必须是 json 类型"}
    return json.dumps(res, ensure_ascii=False)
    

    if name == "main": app.run(port=8888, debug=True)

    7 条回复    2019-12-23 15:40:15 +08:00
    DOUWH
        1
    DOUWH  
    OP
       2019-04-11 18:24:27 +08:00
    在线等,求各位大佬解答~~~
    DOUWH
        2
    DOUWH  
    OP
       2019-04-11 18:43:16 +08:00
    别沉啊,大佬们吃完晚饭快来看啊
    DOUWH
        3
    DOUWH  
    OP
       2019-04-11 18:51:35 +08:00
    解决了,想听听各位大佬的高见
    iloveyby
        5
    iloveyby  
       2019-04-12 11:15:14 +08:00
    最近在学习 asyncio 和 aiohttp 的相关知识, 和 lz 共勉.
    先看错误
    ` RuntimeError: There is no current event loop in thread 'Thread-2'`
    这个说明在 thread-2 的线程中没有事件循环
    asyncio 中,
    先要创建一个 get_event_loop 的对象,
    然后
    需要生成一个类型是 list 的 tasks
    最近把 tasks 放入 run 中进行循环处理

    看 lz 的 run 代码
    ```
    def run(self, name, sku, page_start, page_end, sort, price_start, price_end):
    loop = asyncio.get_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(self.async_run(start_page, end_page, goods_name, rank_type, price))
    ```

    没有发现有 tasks 的任务..感觉问题就出现在这里..
    wuyifar
        6
    wuyifar  
       2019-04-12 17:49:34 +08:00   ❤️ 1
    不要用 asyncio.get_event_loop()
    直接 asyncio.new_event_loop()
    DelayNoMore
        7
    DelayNoMore  
       2019-12-23 15:40:15 +08:00
    @wuyifar 一针见血,解决问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1051 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 19:36 · PVG 03:36 · LAX 12:36 · JFK 15:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.