1

I'm new to threads and am executing a very basic thread example. The following is the program I'm trying to execute:

import os
import thread
import threading

def main():
    t1=thread.start_new_thread(prints,(3,))
    t2=thread.start_new_thread(prints,(5,))
    t1.start()
    t2.start()
    #t1.join()
    #t2.join()

def prints(i):
    while(i>0):
        print "i="+str(i)+"\n"
        i=i-1

if __name__=='__main__':
   main()

When I try and execute, I keep getting the following error (AttributeError: 'int' object has no attribute 'start'):

Traceback (most recent call last):
i=3
  File "thread_1.py", line 19, in <module>

    i=2

i=1
main()

i=5

i=4

i=3

i=2

i=1

  File "thread_1.py", line 8, in main
    t1.start()
AttributeError: 'int' object has no attribute 'start'

As can be seen from the output, both are getting executed but not the way I was expecting it to (which is interleaved prints or something like that). It seems more sequential too. How can I modify/correct my program to get the expected output?

skrowten_hermit
  • 437
  • 3
  • 11
  • 28

1 Answers1

2

From the docs of thread.start_new_thread (boldface mine):

Start a new thread and return its identifier.

So you are actually calling .start() on an int which is obviously not allowed. But you are actually executing the function prints() as you noticed:

The thread executes the function function with the argument list args (which must be a tuple).

This SO question might address some of your questions regarding creating and using threads in Python.

Community
  • 1
  • 1
Idos
  • 15,053
  • 14
  • 60
  • 75
  • 4
    to expand on this, if you want to call start() explicitly, use t = threading.Thread(fn, args, kwargs) t.start() – A Small Shell Script Feb 20 '16 at 16:02
  • Thank you @ASmallShellScript – Idos Feb 20 '16 at 16:03
  • Okay. So, according to you, if I return its identifier, it will execute fine? It would be helpful if you could you throw some more light on this. Also, when I comment out t1.start() and t2.start(), the error is gone but I get only i=3 as print. Understandably because thread was not started right? – skrowten_hermit Feb 20 '16 at 16:31
  • 1
    @skrowten_hermit yes it will execute fine as long as you don't call `start` like you said. When you remove the `start` it works well for me (the output is not "in order" since the two threads run simultaneously). If you want it to print "in order" then a better option is to use what was suggested in the other comment (creating a thread, starting it and using `join`) – Idos Feb 20 '16 at 16:33
  • @Idos : So, how do I return the identifier? – skrowten_hermit Feb 20 '16 at 16:37
  • What do you need the identifier for? You can use [`thread.get_ident`](https://docs.python.org/2/library/thread.html#thread.get_ident) for that anyways – Idos Feb 20 '16 at 16:37
  • Ohhh. Okay. So, using thread is it not possible to see interleaved execution? I'm trying to understand what needs to be done to achieve that. – skrowten_hermit Feb 20 '16 at 16:40
  • I'm not sure what you mean by *see*, what happens is interleaved (in your program). You can use either method to achieve this. If you want *sequential* run you might even want to avoid threads because they are best suited for tasks you want done simultaneously :) – Idos Feb 20 '16 at 16:42