主脚本停止时后台进程的参数错误无效

Mar*_*oon 10 python daemon background-process tweepy python-2.7

我有这个代码通过运行后台进程来获取推文.以下脚本使用subprocess.Popen函数从主脚本运行.这样主脚本将在调用后台进程脚本后停止执行.

def start_listner(unique_id, keyword, limit=200):
    class CustomStreamListener(tweepy.StreamListener):

        def __init__(self, api):
            logger.info('runnning')
            self.api = api
            super(tweepy.StreamListener, self).__init__()

            #setup rabbitMQ Connection


        def on_status(self, status):
            print status.text.encode('utf-8'), "\n"
            #queue the tweet and writes the tweet to the log

        def on_error(self, status_code):
          #some code to not kill the stream

        def on_timeout(self):
          #some code to not kill the stream

    sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
    try:
        logger.info('tracking started')
        logger.info(keyword)
        logger.info(type(keyword))
        kw = keyword
        sapi.filter(track=[kw])  # keeps listening to the streaming api
    except Exception, err:
        logger.info(kw) # fails at this place when main py stops
        logger.info(err)

if __name__ == "__main__":
    logger.info("just now started")
    try:
        a = str(sys.argv[1])
        b = str(sys.argv[2])
        #c = int(sys.argv[5])
        logger.info(a)
        logger.info(b)
    except Exception, err:
        logger.info("inside main")
    start_listner(a, b)
Run Code Online (Sandbox Code Playgroud)

根据这里最高的投票答案,我使用以下主脚本来调用StreamingAnalytics.py(上面的代码)

import time
import subprocess
subprocess.Popen(["python", "StreamingAnalytics.py", 'SriLankaQ', 'lanka'])

print 'I could escape.........'
time.sleep( 15 )
Run Code Online (Sandbox Code Playgroud)

我添加了一个睡眠,因此在此期间推文将成功添加到RabbitMQ队列中.但是一旦主脚本停止,后台进程就会打印出以下错误.

2015-12-22 16:28:16,559 - 主要 - 信息 - {'text':'RT @Dory:lanka唱歌热线bling\xf0\x9f\x98\x82\xf0\x9f\x98\x82'来源':你'Twitter for iPhone'}

2015-12-22 16:28:17,752 - 主要 - 信息 - 兰卡

2015-12-22 16:28:17,752 - 主要 - 信息 - [Errno 22]参数无效

更新: 因为我认为它在传递参数时存在问题,我通过主脚本将它们写入文件并从后台进程文件中读取文件来删除参数的使用.所以,

subprocess.Popen(["python", "StreamingAnalytics.py"])
Run Code Online (Sandbox Code Playgroud)

但仍然会出现同样的错误.使用回溯模块,我可以打印有关此错误的更多信息.

2015-12-24 11:01:16,562 - __main__ - INFO - Traceback (most recent call last):
File "StreamingAnalytics.py", line 84, in <module>
    sapi.filter(track=[keyword])
File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 445, in filter
    self._start(async)
File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 361, in
_start
    self._run()
File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 294, in _run
    raise exception IOError: [Errno 22] Invalid argument
Run Code Online (Sandbox Code Playgroud)

Dim*_*nek 2

你的回溯被 tweetpy 遮盖了。

我的建议:

  • 编辑tweepy/streaming.py
  • 找到两条线exception = ...
  • logging.exception("foobar")在之前或之后添加
  • 再次运行
  • 发布完整的回溯