Multi-threaded programming in Python

Thread class in python is used to define and control an activity. Each of these activities are parts of the program that are executed concurrently in separate threads. In order to create a thread:

  1. Create a sub class of the thread class.
  2. Override the __init__ method. This method is used to initialize the data specific to a thread.
  3. Override the run method. This method defines the behavior of the thread.

The activity of a thread can be launched by calling start() method on the thread object. This method call moves the thread to runnable state, though the thread might not actually be running. A thread stops when it’s run method is terminated.

The thread on which the the main python program runs is called the main thread object. All the thread created are children of the main thread.

Method calls associated with thread object

Each thread can be considered to be an object, that has various methods linked to it, that can perform certain definite action. The thread class defines following methods particular to a thread that can be invoked on them:

  1. start(): Start the thread’s activity.
    Syntax:

    thread.start()

    Description: It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

    This method will raise a RuntimeError if called more than once on the same thread object.

  2. name: A string used for identification purposes only.
    Syntax:

    thread.name

    Description: It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.

  3. run(): Method representing the thread’s activity.
    Syntax:

    thread.run()

    Description: You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

  4. getName(): Returns the name of the current thread.
    Syntax:

    thread.getName()
  5. setName(): Sets a name for the current thread.
    Syntax:

    thread.setName()
  6. is_alive(): Return whether the thread is alive.
    Syntax:

    thread.is_alive()

    Description: This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.

  7. daemon: A boolean value indicating whether this thread is a daemon thread (True) or not (False).
    Syntax:

    thread.daemon

    Description: This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.
    The entire Python program exits when no alive non-daemon threads are left.

  8. isDaemon(): Returns a boolean value indicating whether the current thread is daemon or not.
    Syntax:

    thread.isDaemon()
  9. ident: The ‘thread identifier’ of this thread or None if the thread has not been started.
    Syntax:

    thread.ident

    Description: This is a nonzero integer. See the _thread.get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.

  10. join(timeout=None): TWait until the thread terminates.
    Syntax:

    thread.join()

    Description: This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.

Python program to demonstrate thread objects

filter_none

edit
close

play_arrow

link
brightness_4
code

chevron_right


Output:

MainThread
80
GeeksforGeeks
Identifier of current thread: 139778220697344
85
GFG
Identifier of current thread: 139778212304640
Is thread1 alive?: False
Is thread2 alive?: False
Is thread1 a daemon?: False
80
GeeksforGeeks
Identifier of current thread: 139778244499264
MainThread
GFG_Thread
Exiting the main thread

Reference:

This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.