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

时间:2015-12-22 11:12:35

标签: python python-2.7 daemon background-process tweepy

我有这段代码通过运行后台进程来获取推文。使用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)

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

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

print 'I could escape.........'
time.sleep( 15 )

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

  

2015-12-22 16:28:16,559 - main - INFO - {' text':' RT @Dory:lanka   唱热线bling \ xf0 \ x9f \ x98 \ x82 \ xf0 \ x9f \ x98 \ x82   '来源':u' Twitter for iPhone'}

     

2015-12-22 16:28:17,752 - main - INFO - lanka

     

2015-12-22 16:28:17,752 - main - INFO - [Errno 22]参数无效

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

subprocess.Popen(["python", "StreamingAnalytics.py"])

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

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

1 个答案:

答案 0 :(得分:2)

你的追溯被tweetpy掩盖了。

我的建议:

  • 修改tweepy/streaming.py
  • 找到两行exception = ...
  • 之前或之后添加logging.exception("foobar")
  • 再次跑步
  • 发布完整追溯