简体   繁体   English

在python中使用具有不同功能的相同线程

[英]Using the same thread with different functions in python

I'm new to threading. 我是线程新手。 I wrote this program where I'm trying to execute 2 different functions using threads. 我写了这个程序,试图使用线程执行2个不同的函数。 I tried to run different functions using the same thread by changing the target and args parameters one after the other : 我试图通过args更改targetargs参数来使用同一线程运行不同的功能:

import threading
import datetime
import Queue

a=Queue.Queue()
b=Queue.Queue()
q=Queue.Queue()

class MyThread(threading.Thread):
    def __init__(self,q):
        threading.Thread.__init__(self)
        self.que=q
        t1=threading.Thread(target=self.prints,args=(4,))
        t2=threading.Thread(target=self.printy,args=(6,self.que,))
        t1.start()
        t2.start()
        item=self.que.get()
        print(item)
        print "*"*30
        it=item*2
        print(it)
        t1.join()

    def main(self):
        t3=threading.Thread(target=self.prints,args=(3,))
        t4=threading.Thread(target=self.printy,args=(5,self.que,))
        t3.start()
        t4.start()
        item=self.que.get()
        print(item)
        print "#"*30
        it=item*2
        print(it)
        t2=threading.Thread(target=self.prints,args=(8,))
        t4=threading.Thread(target=self.prints,args=(7,))
        t2.start()
        t4.start()
        t2.join()
        t3.join()
        t4.join()

    def prints(self,i):
        while(i>0):
            print "i="+str(i)+" "+str(datetime.datetime.now().time())+"\n"
            i=i-1

    def printy(self,i,b):
        r=0
        while(i<10):
            print "i="+str(i)+" "+str(datetime.datetime.now().time())+"\n"
            i=i+1
            r=r+i
        self.que.put(r)

if __name__=='__main__':
    MyThread(a).main()

It executed without throwing any error and also gave me the output I wanted, but I wanted to know if : 它执行时没有抛出任何错误,并且还提供了我想要的输出,但是我想知道是否:

  1. This is the right way to do so? 这是正确的方法吗? Otherwise, What is the right way to run multiple functions using the same thread as a reusable unit?. 否则,使用同一线程作为可重用单元来运行多个功能的正确方法是什么?
  2. In a simple program like this, it seems harmless enough but what about more complex programs with many more functions? 在像这样的简单程序中,看起来似乎没有什么害处,但是具有更多功能的更复杂程序又如何呢? Any potential problems that could arise? 可能会出现任何潜在问题吗?
from multiprocessing import Pool

def f(x):
    return x*x*x

def b(x):
    return x*x    

if __name__ == '__main__':
    p = Pool(5)
    print(p.map(f, [1, 2, 3]))
    print(p.map(b, [1, 2, 3]))

>> [1, 8, 27]
>> [1, 4, 9]

Please notice that you can't run this example in an ipython session! 请注意,您无法在ipython会话中运行此示例!

edit with threading: 用线程编辑:

import threading
import queue
q = queue.deque()


def niceFunc1(data):
  return reversed(data)

def niceFunc2(data):
  return "Hey there :%s" % data



q.append( (niceFunc1,"gude") )
q.append( (niceFunc1,"test") )
q.append( (niceFunc2,"test") )
q.append( (niceFunc2,"123") )


def worker():
  for each in q:
    print(each[0](each[1]))


t=threading.Thread(target=worker)
t.start()

t2=threading.Thread(target=worker)
t2.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM