添加一个线程管理的flag,决定是用户来管理主线程还是由itchat自动管理。 (#181)

This commit is contained in:
tempdban
2016-12-14 10:06:34 +08:00
committed by LittleCoder
parent fa4dc1dcbf
commit 85044108ee
3 changed files with 27 additions and 16 deletions
+7 -4
View File
@@ -191,7 +191,7 @@ def show_mobile_login(self):
r = self.s.post(url, data=json.dumps(data), headers=headers)
return ReturnValue(rawResponse=r)
def start_receiving(self, exitCallback=None):
def start_receiving(self, exitCallback=None, getReceivingFnOnly=False):
self.alive = True
def maintain_loop():
retryCount = 0
@@ -229,9 +229,12 @@ def start_receiving(self, exitCallback=None):
exitCallback()
else:
logger.info('LOG OUT!')
maintainThread = threading.Thread(target = maintain_loop)
maintainThread.setDaemon(True)
maintainThread.start()
if getReceivingFnOnly:
return maintain_loop
else:
maintainThread = threading.Thread(target=maintain_loop)
maintainThread.setDaemon(True)
maintainThread.start()
def sync_check(self):
url = '%s/synccheck' % self.loginInfo.get('syncUrl', self.loginInfo['url'])
+16 -9
View File
@@ -1,4 +1,4 @@
import logging, traceback, sys
import logging, traceback, sys, threading
try:
import Queue
except ImportError:
@@ -84,14 +84,21 @@ def msg_register(self, msgType, isFriendChat=False, isGroupChat=False, isMpChat=
self.functionDict['FriendChat'][_msgType] = fn
return _msg_register
def run(self, debug=False):
def run(self, debug=False, blockThread=True):
logger.info('Start auto replying.')
if debug:
set_logging(loggingLevel=logging.DEBUG)
try:
while self.alive: self.configured_reply()
except KeyboardInterrupt:
if self.useHotReload: self.dump_login_status()
self.alive = False
logger.debug('itchat received an ^C and exit.')
print('Bye~')
def reply_fn():
try:
while self.alive: self.configured_reply()
except KeyboardInterrupt:
if self.useHotReload: self.dump_login_status()
self.alive = False
logger.debug('itchat received an ^C and exit.')
print('Bye~')
if blockThread:
reply_fn()
else:
replyThread = threading.Thread(target=reply_fn)
replyThread.setDaemon(True)
replyThread.start()
+4 -3
View File
@@ -112,11 +112,12 @@ class Core(object):
it is defined in components/login.py
'''
raise NotImplementedError()
def start_receiving(self, finishCallback=None):
def start_receiving(self, exitCallback=None, getReceivingFnOnly=False):
''' open a thread for heart loop and receiving messages
for options:
- finishCallback: callback after logged out
- exitCallback: callback after logged out
- it contains calling of logout
- getReceivingFnOnly: if True thread will not be created and started. Instead, receive fn will be returned.
for processing:
- messages: msgs are formatted and passed on to registered fns
- contact : chatrooms are updated when related info is received
@@ -432,7 +433,7 @@ class Core(object):
return a specific decorator based on information given
'''
raise NotImplementedError()
def run(self, debug=True):
def run(self, debug=True, blockThread=True):
''' start auto respond
for option
- debug: if set, debug info will be shown on screen