跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 开发与实施交流
  4. Python多线程(threading)

Python多线程(threading)

已定时 已固定 已锁定 已移动 Odoo 开发与实施交流
4 帖子 2 发布者 3.3k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • G 离线
    G 离线
    ghotiv
    写于 最后由 编辑
    #1
    <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 />
    
    1 条回复 最后回复
    0
    • mrshellyM 离线
      mrshellyM 离线
      mrshelly
      写于 最后由 编辑
      #2

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

      1 条回复 最后回复
      0
      • G 离线
        G 离线
        ghotiv
        写于 最后由 编辑
        #3

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

        1 条回复 最后回复
        0

        • 登录

        • 没有帐号? 注册

        • 登录或注册以进行搜索。
        • 第一个帖子
          最后一个帖子
        0
        • 版块
        • 标签
        • 热门
        • 用户
        • 群组