diff --git a/itchat/components/login.py b/itchat/components/login.py index 5663c7f..709b830 100644 --- a/itchat/components/login.py +++ b/itchat/components/login.py @@ -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']) diff --git a/itchat/components/register.py b/itchat/components/register.py index 3afe8c3..47bd356 100644 --- a/itchat/components/register.py +++ b/itchat/components/register.py @@ -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() diff --git a/itchat/core.py b/itchat/core.py index 44ee1e7..3ce410d 100644 --- a/itchat/core.py +++ b/itchat/core.py @@ -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