2

I'm new to threading. I'm using 4 threads to run my function on 4 different things. Now I need to set a flag in my function which will do different calculations on each. What's the best way to do it? I'm trying to do this, is this correct. If there is any better way please suggest.

def func(i,flag):
while True:
    if flag==0:
       something
    else:
       something else
       flag-=1

flag=["0","0","0","0"]

try:
   thread.start_new_thread( func, (10,flag[0], ) )
   thread.start_new_thread( func, (11,flag[1], ) )
   thread.start_new_thread( func, (12,flag[2], ) )
   thread.start_new_thread( func, (13,flag[3], ) )

except:
   print "Error: unable to start thread"



#after sometime

flag[0]+=1
flag[2]+=1
5
  • I'm not sure what you are actually trying to accomplish. Do you really need to pass a value to a running thread, or are you wanting each thread to perform a different action? Commented Aug 1, 2015 at 6:22
  • Use queues to interchange data from main loop to threads. Pass a queue object in the thread init and read in while loop. Commented Aug 1, 2015 at 6:23
  • Looks ok to me. I parameterize a worker function by int n, which runs fun[n]() in a list of functions. Commented Aug 1, 2015 at 6:26
  • @weirdev yes, I need to pass a value to one or more threads. Initially all threads performing same function on four different things. After I send a flag one or more of the threads perform something different for sometime and then they will return back. Commented Aug 1, 2015 at 6:47
  • @SmartElectron, I've tried that. Is there any better way to do it. Commented Aug 1, 2015 at 6:48

1 Answer 1

1

I see the following problems:

  1. Use threading instead of thread. thread goes away in Python3.
  2. You initialize flag to a list of strings, but then you increment each element by adding 1 (an integer). That won't work.
  3. The thread has no visibility to the global variable flag. The changes that you make to the list "after sometime" don't affect the local variable flag in func. They are two different objects. Modifying a list element does not change the value originally passed into func since it's a simple string; strings in Python have value (not reference) semantics.

Try the following:

flag=[0,0,0,0]
def func(i,thread_index):
    if flag[thread_index] == 0:
       something
    else:
       something else
       flag[thread_index] -= 1


try:
   thread.start_new_thread( func, (10,0))
   thread.start_new_thread( func, (11,1))
   thread.start_new_thread( func, (12,2))
   thread.start_new_thread( func, (13,3))

except:
   print "Error: unable to start thread"

#after sometime

flag[0] += 1
flag[2] += 1

This is a rather crude but it does what you want. flag is a global list, and each thread accesses a unique index inside that list. When the main thread accesses the list you see the change in func because it accesses each element through the enclosing container (list) object.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. Is there any other way than this to solve the issue. I'm ready to use any other way better than this. TIA.
I'm not sure how to answer this. There are lots of ways to do it, including Queues, Locks, Events and all the other apparatus documented in the threading module. There's nothing inherently wrong with the method discussed here, it's just not very generic. I recommend "Python in a Nutshell" by Alex Martelli for its clear explanations and good examples.

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.