Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

Python多线程(threading)



  • <br /># -*- coding: utf-8 -*- <br />#author:ghoti<br />&#039;&#039;&#039;自定义多线程运行函数,可定义同时运行个数&#039;&#039;&#039;<br />import threading,time<br />from time import sleep,ctime<br />class My_Thead:<br />&nbsp; &nbsp; &#039;&#039;&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 多线程运行函数,将all_list切片处理,num为进程数,all_list小于num,单线程运行,<br />&nbsp; &nbsp; sleep_time为单个进程(一个all_list切片)运行后停留时间(方便调试),<br />&nbsp; &nbsp; myfunc为处理每个切片的函数,对all_list切片,在处理<br />&nbsp; &nbsp; &#039;&#039;&#039;<br />&nbsp; &nbsp; def __init__ (self, all_list, num, sleep_time=0, myfunc=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; self.all_list = all_list<br />&nbsp; &nbsp; &nbsp; &nbsp; self.len_list = len(all_list)<br />&nbsp; &nbsp; &nbsp; &nbsp; self.num = num&nbsp; &nbsp; #进程数,实际进程为可能为num+1<br />&nbsp; &nbsp; &nbsp; &nbsp; if len(all_list)&lt;num:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.num = 1<br />&nbsp; &nbsp; &nbsp; &nbsp; self.m = self.len_list/self.num<br />&nbsp; &nbsp; &nbsp; &nbsp; if not self.m:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.m = 1<br />&nbsp; &nbsp; &nbsp; &nbsp; self.loops=range(0 , self.len_list, self.m)<br />&nbsp; &nbsp; &nbsp; &nbsp; self.sleep_time = sleep_time<br />&nbsp; &nbsp; &nbsp; &nbsp; self.myfunc = myfunc<br />&nbsp; &nbsp; def loop (self,nloop,n,m):<br />&nbsp; &nbsp; &nbsp; &nbsp; self.myfunc(n,m)<br />&nbsp; &nbsp; &nbsp; &nbsp; sleep(self.sleep_time)<br />&nbsp; &nbsp; def __call__ (self):<br />&nbsp; &nbsp; &nbsp; &nbsp; if not self.all_list:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return False<br />&nbsp; &nbsp; &nbsp; &nbsp; threads=&#91;]<br />&nbsp; &nbsp; &nbsp; &nbsp; nloops=range(len(self.loops))<br />&nbsp; &nbsp; &nbsp; &nbsp; for i in nloops:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t=threading.Thread(target=self.loop,args=(i, self.loops[i], self.m))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threads.append(t)<br />&nbsp; &nbsp; &nbsp; &nbsp; for i in nloops:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threads[i].start()<br />&nbsp; &nbsp; &nbsp; &nbsp; for i in nloops:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threads[i].join()<br /><br />&#039;&#039;&#039;<br />##how to use<br />#from my_thread import My_Thead<br />all_list = range(103)<br />def myfunc(n,m):<br />&nbsp; &nbsp; part_list = all_list[n:n+m]<br />&nbsp; &nbsp; for i in part_list:<br />&nbsp; &nbsp; &nbsp; &nbsp; print i,<br />My_Thead(range(103),5,myfunc=myfunc)()<br />&#039;&#039;&#039;<br /><br />&#039;&#039;&#039;&nbsp; &nbsp; &nbsp; &nbsp; <br />## use in oe<br />&nbsp; &nbsp; &nbsp; &nbsp; def myfunc(n,m):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; part_list = rsps[n:n+m]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for rsp in part_list:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.action_do_product(cr, uid, ids, rsp, context=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; from openerp.addons.basefunc.my_thread import My_Thead<br />&nbsp; &nbsp; &nbsp; &nbsp; My_Thead(rsps,10,myfunc=myfunc)()<br />&#039;&#039;&#039;<br />
    


  • OE 使用多线程  可以看看  cron 的处理代码...



  • 恩恩,这个写的通用性强些,不仅在oe,其它地方也可以用