0

I'm new to threading. I wrote this program where I'm trying to execute 2 different functions using threads. I tried to run different functions using the same thread by changing the target and args parameters one after the other :

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?

1 Answer 1

2
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!

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()
Sign up to request clarification or add additional context in comments.

4 Comments

So, this can be done only using multiprocessing class and not threading class? I wanted to know if there is a safe way of doing this using threading.
You could have a queue where the thread is pulling a (function,data) tuple. Then you could execute the function with the provided data.
Could you throw some more light on the same with an example please?
please see my edit. I've not created the class but you should get the idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.