python脚本在一段时间后停止

时间:2015-02-08 01:06:50

标签: python amazon-ec2 nohup http-request

我在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使用屏幕打开程序。它又停了下来。如果我按&#34; CTRL + C&#34;为了阻止它,这就是我得到的:

^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)

任何线索?我应该添加超时吗?

1 个答案:

答案 0 :(得分:0)

收集程序的所有输出,你会发现有关错误的非常好的提示。也就是说,绝对收集stdoutstderr。为此,您可能希望像这样调用您的程序:

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

在log.outerr文件中收集stdout和stderr,然后启动与tty和后台解耦的程序。在你的计划停止工作后调查log.outerr可能会非常有启发性,我猜。

相关问题