Python 高度定制化自己的线程类和进程类代码,获取启动进程或线程方法的结果(兼容Py2和Py3)

#encoding=utf-8 from threading import Thread from multiprocessing import Process import multiprocessing ''' Author liuyancong ''' class My_Job_With_Thread(Thread): def run(self): t_flag = True b = None try: b = self._Thread__target(*self._Thread__args, **self._Thread__kwargs) except AttributeError as e: b = self._target(*self._args, **self._kwargs) t_flag = False finally: if t_flag: del self._Thread__target;del self._Thread__args;del self._Thread__kwargs else: del self._target, self._args, self._kwargs # TODO 你可以在这做你想做的事情 print(b) class My_Job_With_Process(Process): def run(self): res = None if self._target: res = self._target(*self._args, **self._kwargs) print (res) #TODO 你可以在这做你想做的事情 def k(b): print('K方法开始执行') return b if __name__ == '__main__': My_Job_With_Process(target=k, args=('lyc_Process',)).start() My_Job_With_Thread(target=k,args=('lyc_Thread',)).start() print('end')

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgffwd.html