【问题标题】:python script stops after a whilepython脚本在一段时间后停止
【发布时间】:2015-02-08 01:06:49
【问题描述】:

我在 Amazon EC2 服务器上有一个 python 脚本,它从两个不同的服务器(使用 urllib 和 http.request)请求数据,然后将该数据记录在一个文本文件中。它必须运行很长时间。我正在使用 nohup 让它在后台运行。

问题是,它会在一段时间后停止(有时会持续 24 小时,有时会持续 2 小时,情况会有所不同)。我没有收到错误消息或任何东西。它只是停止并且收到的最后一串字符只是作为不完整的字符串保存在文本文件中(只是可以从远程服务器读取的信息)。

什么可能导致这个问题?

这是我的代码:

import urllib3 #  sudo pip install urllib3 --upgrade
import time

urllib3.disable_warnings() # Disable urllib3 warnings about unverified connections
http = urllib3.PoolManager()

f = open('okcoin.txt', 'w')
f2 = open('bitvc.txt', 'w')

while True:
    try:
        r = http.request("GET","https://www.okcoin.com/api/v1/future_ticker.do?symbol=btc_usd&contract_type=this_week")
        r2 = http.request("GET","http://market.bitvc.com/futures/ticker_btc_week.js ")
    except: # catch all exceptions
        continue

    #Status codes of 200 if it got an OK from the server
    if r.status != 200 or r2.status != 200 or r.data.count(',') < 5 or r2.data.count(',') < 5: # avoids blank data, there should be at least 5 commas so that it's correct data
        continue; # Try to read again if there was a problem with one reading

    received = str(time.time()) # Timestamp of when the information was received to the server running this python code

    data = r.data + "," + received + "\r\n"
    data2 = r2.data + "," + received + "\r\n"        

    print data,r.status
    print data2, r.status

    f.write(data)
    f2.write(data2)

    time.sleep(0.5)
    f.flush() #flush files
    f2.flush()
f.close()
f2.close()

编辑:我通过 ssh 使用屏幕打开了程序。它又停了下来。如果我按“CTRL+C”停止它,这就是我得到的:

^CTraceback (most recent call last):
  File "tickersave.py", line 72, in <module>
    r2 = http.request("GET","http://market.bitvc.com/futures/ticker_btc_week.js")
  File "/usr/local/lib/python2.7/dist-packages/urllib3/request.py", line 68, in request
    **urlopen_kw)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/request.py", line 81, in request_encode_url
    return self.urlopen(method, url, **urlopen_kw)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/poolmanager.py", line 153, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 541, in urlopen
    **response_kw)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/response.py", line 284, in from_httplib
    **response_kw)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/response.py", line 104, in __init__
    self._body = self.read(decode_content=decode_content)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/response.py", line 182, in read
    data = self._fp.read()
  File "/usr/lib/python2.7/httplib.py", line 551, in read
    s = self._safe_read(self.length)
  File "/usr/lib/python2.7/httplib.py", line 658, in _safe_read
    chunk = self.fp.read(min(amt, MAXAMOUNT))
  File "/usr/lib/python2.7/socket.py", line 380, in read
    data = self._sock.recv(left)

有什么线索吗?我应该添加超时还是什么?

【问题讨论】:

    标签: python amazon-ec2 nohup http-request


    【解决方案1】:

    收集你程序的所有输出,你会发现一个很好的提示关于哪里出了问题。也就是说,一定要收集stdout和stderr。为此,您可能希望这样调用您的程序:

    $ nohup python script.py > log.outerr 2>&1 &
    

    这会在 log.outerr 文件中收集 stdout 和 stderr,并在后台和 tty 中启动您的程序。在你的程序停止工作后调查log.outerr 可能会很有启发性,我猜。

    【讨论】:

    • 又发生了。停了。但是 log.outerr 文件没有任何错误或任何东西!只有输出存在(从服务器请求的信息)。最后几行根本不完整,但没有错误或任何东西!你有什么线索吗?
    • 一个外部事件意外地杀死了你的脚本,以沉重的方式,所以它没有机会向 stdout/err 报告任何内容。这样的事件将是 SIGKILL,接收进程没有机会响应。也就是说,您需要扩大调试范围,不仅要看脚本本身,还要看它运行的整个环境。
    • 我明白了...您认为这与 Amazon EC2 服务器有关吗?我基本上在我的计算机和远程服务器上同时运行代码。我计算机上的脚本仍在运行,但是亚马逊的脚本停止了...... Linux 服务器中是否有任何可能导致这种情况的东西?谢谢!
    • 这基本上是我们无法帮助您的地方,因为问题太广泛了。那只能用“动手”的方法来调试。您需要调查运行脚本的 Python 进程发生了什么。
    • Jan,我编辑了问题并添加了更多细节。即使我使用尽可能多的异常处理程序尽可能多地调试程序,我仍然没有收到特定错误。它似乎挂在 http 请求中,但当我按 CTRL+C 关闭它时,它就是这样显示的。
    猜你喜欢
    • 2012-12-09
    • 1970-01-01
    • 2013-12-21
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多