python - Exception in thread Thread-4 - finding the reason or catch the exception -
i'm running script on server in order send push notifications on new mail arrival. works great script crash error below. don't know why it's crashing , can't reproduce on own. when fails won't go except section. i've tried: except self.abort:
except m.abort:
except m.error:
any idea why it's crashing?
is possible, short term solution, kill threads , restart script
except
section?
.......
import socket, ssl, json, struct, re import imaplib2, time threading import * # enter gmail login details here user="your.name@gmail.com" password="your-gmail-password" # enter device token here devicetoken = 'xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx ' devicetoken = devicetoken.replace(' ','').decode('hex') currentbadgenum = -1 def getunseen(): (resp, data) = m.status("inbox", '(unseen)') print data return int(re.findall("unseen (\d)*\)", data[0])[0]) def sendpushnotification(badgenum): global currentbadgenum, devicetoken if badgenum != currentbadgenum: currentbadgenum = badgenum thepayload = { 'aps': { # 'alert':'hello world!', 'sound':'', 'badge': badgenum, }, 'test_data': { 'foo': 'bar' }, } thecertfile = 'certfile.pem' thehost = ('gateway.sandbox.push.apple.com', 2195) data = json.dumps(thepayload) theformat = '!bh32sh%ds' % len(data) thenotification = struct.pack(theformat, 0, 32, devicetoken, len(data), data) ssl_sock = ssl.wrap_socket(socket.socket(socket.af_inet, socket.sock_stream), certfile=thecertfile) ssl_sock.connect(thehost) ssl_sock.write(thenotification) ssl_sock.close() print "sent push alert." # threading object waiting on # event class idler(object): def __init__(self, conn): self.thread = thread(target=self.idle) self.m = conn self.event = event() def start(self): self.thread.start() def stop(self): # neat trick make thread end. took me # while figure 1 out! self.event.set() def join(self): self.thread.join() def idle(self): # starting unending loop here while true: # part of trick make loop stop # when stop() command given if self.event.isset(): return self.needsync = false # callback method gets called when new # email arrives. basic, that's good. def callback(args): if not self.event.isset(): self.needsync = true self.event.set() # actual idle call. returns immediately, # since it's asynchronous. self.m.idle(callback=callback) # waits until event set. event # set callback, when server 'answers' # idle call , callback function gets # called. self.event.wait() # because function sets needsync variable, # helps escape loop without doing # if stop() called. kinda neat # solution. if self.needsync: self.event.clear() self.dosync() # method gets called when new email arrives. def dosync(self): print "got event!" numunseen = getunseen() sendpushnotification(numunseen) try: # set following 2 lines creds , server print 'starting' global m m = imaplib2.imap4_ssl("imap.gmail.com") m.login(user, password) # need out of auth state, select # inbox. #m.list() m.select('inbox', readonly=true) numunseen = getunseen() gettext() #print m.status("inbox", '(unseen)') # start idler thread sendpushnotification(numunseen) idler = idler(m) idler.start() # sleep forever, 1 minute @ time while true: time.sleep(60) except self.abort: print 'we had problem, dont worry! ill fix it!' idler = idler(m) finally: # clean up. idler.stop() idler.join() m.close() # important! m.logout()
.....................
.....................
17:35.99 imap.gmail.com reader last 20 log messages: 17:36.02 imap.gmail.com handler last 20 log messages: got event! exception in thread thread-4: traceback (most recent call last): file "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner self.run() file "/usr/local/lib/python2.7/threading.py", line 504, in run self.__target(*self.__args, **self.__kwargs) file "serverx.py", line 229, in idle self.dosync() file "serverx.py", line 235, in dosync numunseen = getunseen() file "serverx.py", line 150, in getunseen (resp, data) = m.status("inbox", '(unseen)') file "/home/myuser/lib/python2.7/imaplib2/imaplib2.py", line 1121, in status return self._simple_command(name, mailbox, names, **kw) file "/home/myuser/lib/python2.7/imaplib2/imaplib2.py", line 1607, in _simple_command return self._command_complete(self._command(name, *args), kw) file "/home/myuser/lib/python2.7/imaplib2/imaplib2.py", line 1305, in _command raise self.abort('connection closed') abort: connection closed
try catching imaplib2.imap4.abort
:
try: # ... except imaplib2.imap4.abort: # oh no! error!
Comments
Post a Comment