在Flask中运行python子进程

问题描述 投票:0回答:1

我有一个以这种方式运行爬行过程的Flask Web应用程序:

在终端选项卡1上:

$ cd /path/to/scraping
$ scrapyrt 

http://scrapyrt.readthedocs.io/en/latest/index.html

在终端选项卡2上:

$ python app.pp 

app.py

params = {
          'spider_name': spider,
          'start_requests':True
          }

        response = requests.get('http://localhost:9080/crawl.json', params)
        print ('RESPONSE',response)
        data = json.loads(response.text)

哪个有效。


现在我想把所有东西都搬到qazxsw poi,为此我试过了:

app.py

这开始扭曲,像这样:

        import subprocess
        from time import sleep

        try:
            subprocess.check_output("scrapyrt", shell=True, cwd='path/to/scraping')
        except subprocess.CalledProcessError as e:
            raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

        sleep(3)

params = {
          'spider_name': spider,
          'start_requests':True
           }

        response = requests.get('http://localhost:9080/crawl.json', params)
        print ('RESPONSE',response)
        data = json.loads(response.text)

但爬行过程挂起,不会通过。

我在这里想念的是什么?

flask scrapy subprocess
1个回答
0
投票

您是否考虑过使用APScheduler或类似的调度程序?您可以使用crons或间隔运行代码,并且它与Flask很好地集成。

看看这里:2018-02-03 17:29:35-0200 [-] Log opened. 2018-02-03 17:29:35-0200 [-] Site starting on 9080 2018-02-03 17:29:35-0200 [-] Starting factory <twisted.web.server.Site instance at 0x104effa70>

这是一个例子:

http://apscheduler.readthedocs.io/en/stable/userguide.html
© www.soinside.com 2019 - 2024. All rights reserved.