繁体   English   中英

一段时间后python脚本停止

[英]python script stops after a while

我在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)

有线索吗? 我应该添加超时之类的吗?

收集程序的所有输出,您会发现有关错误的很好提示。 也就是说,一定要收集stdoutstderr 为此,您可能需要这样调用程序:

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

这会将stdout和stderr都收集在文件log.outerr中,并启动从tty和后台解耦的程序。 我猜,在程序停止工作之后,调查log.outerr可能会log.outerr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM