1
fdict= {0: fun1(), 1: fun2()}

# approach 1 :  working fine, printing string
print fdict[random.randint(0,1)]

# approach 2  calling
thread.start_new_thread(fdict[random.randint(0,1)],())

#I also tried following approach
fdict= {0: fun1, 1: fun2}
thread.start_new_thread(fdict[random.randint(0,1)](),())

fun1 and fun2 are returning strings. I am able to call these functions using approach 1 but not able to call using approach 2. Getting error as shown below. But approach 1 already proved that these are callable.

thread.start_new_thread(fdict[random.randint(0,1)],())

TypeError: first arg must be callable

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
user1423015
  • 593
  • 1
  • 4
  • 12

1 Answers1

1

The values of fdict are not functions; they are values returned from func1() and func2() respectively.

>>> fdict = {0: fun1, 1: fun2}
>>> thread.start_new_thread(fdict[random.randint(0,1)], ())

thread is a very low level library where there is no way of joining threads so when your main program finishes before any thread finishes executing its task, you'll be likely to get errors.

You should use threading.Thread class to prevent these kind of issues from happening:

>>> from threading import Thread

>>> fdict = {0: fun1, 1: fun2}
>>> t = Thread(target=fdict[random.randint(0,1)], args=())
>>> t.deamon = True
>>> t.start()
>>> t.join() # main program will wait for thread to finish its task.

You can see the threading documentation for more information.

Hope this helps.

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
  • @user1423015 did you test it out like above? – Ozgur Vatansever Jul 21 '15 at 02:56
  • No ozgur, I tried the first line as you specified but in second line i included (), I did not try the second line as you given, will try now. – user1423015 Jul 21 '15 at 03:00
  • Getting error -- "Unhandled exception in thread started by sys.excepthook is missing lost sys.stderr" looking at it. I did not try in try block, will try and let you know – user1423015 Jul 21 '15 at 03:03
  • The error is because the main thread exists before your thread finishes executing its task. why don't you use `threading.Thread` instead? – Ozgur Vatansever Jul 21 '15 at 03:04
  • because that just i put a print statement at starting line, not entered to function start line too – user1423015 Jul 21 '15 at 03:06
  • thanks for your explanation, i will try this approach and let you know. but my fun1 and fun2 are single liners and main program runs for 100 iterations. So I am doubting. But I will try and surely will let u know the result. thanks again. – user1423015 Jul 21 '15 at 03:22
  • thanks ozgur, it's worked. I marked your answer as useful. can you please let me know how to print the strings returned by these functions in main thread. both functions are returning strings. As of now, I confirmed that functions are calling by placing print statements in function definitions. – user1423015 Jul 21 '15 at 03:33
  • You can't return the value from a function executed by a thread. Threads just execute the method you pass to them and that's all. You should take a look at [`multiprocessing`](http://stackoverflow.com/questions/6893968/how-to-get-the-return-value-from-a-thread-in-python) module if you want to use the returned value in main thread. – Ozgur Vatansever Jul 21 '15 at 03:37