gpt4 book ai didi

python - 主脚本停止时后台进程出现无效参数错误

转载 作者:太空狗 更新时间:2023-10-29 19:28:36 24 4
gpt4 key购买 nike

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

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

2015-12-22 16:28:16,559 - main - INFO - {'text': 'RT @Dory: lanka singing Hotline bling \xf0\x9f\x98\x82\xf0\x9f\x98\x82 'source': 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] Invalid argument

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

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

最佳答案

你的回溯被 tweetpy 掩盖了。

我的建议:

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

关于python - 主脚本停止时后台进程出现无效参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34414561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com