Skip to content
  • Categories
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Brite
  • 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

  • Default (Flatly)
  • No Skin
Collapse

Odoo 中文社区

  1. Home
  2. Categories
  3. Odoo 开发与实施交流
  4. Python多线程(threading)

Python多线程(threading)

Scheduled Pinned Locked Moved Odoo 开发与实施交流
4 Posts 2 Posters 4.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    ghotiv
    wrote on last edited by
    #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 Reply Last reply
    0
    • M Offline
      M Offline
      mrshelly
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • G Offline
        G Offline
        ghotiv
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Tags
        • Popular
        • Users
        • Groups