我有一個簡單的python腳本,用於更新數據庫中justin.tv流的狀態。這是一個基於Django的Web應用程序。此腳本在將其移至我的生產服務器之前完美運行,但現在遇到超時或凍結問題。我已經通過添加try/except塊並使腳本重試來解決超時問題,但我仍然無法弄清楚凍結問題。Python腳本在套接字連接期間無限凍結
我知道它凍結在線streamOnline = manager.getStreamOnline(stream.name, LOG)
。這與socket.timeout
異常發生的地方是一樣的。然而有些時候,它永遠鎖定。我無法想象python會無限凍結的場景。這是凍結腳本的代碼。我正在鏈接下面的website.networkmanagers,以及我正在使用的oauth和justin.tv python庫。
import sys, os, socket
LOG = False
def updateStreamInfo():
# Set necessary paths
honstreams = os.path.realpath(os.path.dirname(__file__) + "../../../")
sys.path.append(honstreams)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
# Import necessary moduels
from website.models import Stream, StreamInfo
from website.networkmanagers import get_manager, \
NetworkManagerReturnedErrorException
# Get all streams
streams = Stream.objects.all()
try:
# Loop through them
for stream in streams:
skipstream = False
print 'Checking %s...' % stream.name,
# Get the appropriate network manager and
manager = get_manager(stream.network.name)
# Try to get stream status up to 3 times
for i in xrange(3):
try:
streamOnline = manager.getStreamOnline(stream.name, LOG)
break
except socket.error as e:
code, message = e
# Retry up to 3 times
print 'Error: %s. Retrying...'
# If this stream should be skipped
if(skipstream):
print 'Can\'t connect! Skipping %s' % stream.name
continue
# Skip if status has not changed
if streamOnline == stream.online:
print 'Skipping %s because the status has not changed' % \
stream.name
continue
# Save status
stream.online = streamOnline
stream.save()
print 'Set %s to %s' % (stream.name, streamOnline)
except NetworkManagerReturnedErrorException as e:
print 'Stopped the status update loop:', e
if(__name__ == "__main__"):
if(len(sys.argv) > 1 and sys.argv[1] == "log"):
LOG = True
if(LOG): print "Logging enabled"
updateStreamInfo()
networkmanagers.py
oauth.py
JtvClient.py
腳本凍結
FOO @欄的例子:/.../ honstreams/honstreams#蟒蛇網站/腳本/ updateStreamStatus。 py
正在檢查angrytestie ...正在跳過angrytestie,因爲st atus尚未更改
檢查chustream ...跳過chustream,因爲狀態沒有改變
檢查cilantrogamer ...跳過cilantrogamer,因爲狀態沒有改變
| < - 插入符就坐落在這裏閃爍無限
有趣的更新
每次它凍結時間,我送一個鍵盤中斷,這是在同一行中socket.py:
[email protected]:/home/honstreams/honstreams# python website/scripts/updateStreamStatus.py
Checking angrytestie... Skipping angrytestie because the status has not changed
Checking chustream... Skipping chustream because the status has not changed
^CChecking cilantrogamer...
Traceback (most recent call last):
File "website/scripts/updateStreamStatus.py", line 64, in <module>
updateStreamInfo()
File "website/scripts/updateStreamStatus.py", line 31, in updateStreamInfo
streamOnline = manager.getStreamOnline(stream.name, LOG)
File "/home/honstreams/honstreams/website/networkmanagers.py", line 47, in getStreamOnline
return self.getChannelLive(channelName, log)
File "/home/honstreams/honstreams/website/networkmanagers.py", line 65, in getChannelLive
response = client.get('/stream/list.json?channel=%s' % channelName)
File "/home/honstreams/honstreams/website/JtvClient.py", line 51, in get
return self._send_request(request, token)
File "/home/honstreams/honstreams/website/JtvClient.py", line 90, in _send_request
return conn.getresponse()
File "/usr/lib/python2.6/httplib.py", line 986, in getresponse
response.begin()
File "/usr/lib/python2.6/httplib.py", line 391, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.6/httplib.py", line 349, in _read_status
line = self.fp.readline()
File "/usr/lib/python2.6/socket.py", line 397, in readline
data = recv(1)
KeyboardInterrupt
有什麼想法?
我已經更新了我的問題 - 你能再看一次嗎? – Hubro