python thread management

Started by ggnfs000, January 14, 2017, 06:02:07 PM

Previous topic - Next topic

ggnfs000

python thread management

Python thread management example. Here in this example, I have attempted to do something: two thread communicate to each other, but in terms of threads: create two threads 1 and 2 and tell thread1 tell thread2 something and as a result thread2 takes some action. This probably is quite rudimentary method compared to sophisticated IPC mechanism where threads or process communicate through semaphore or locks.

Specifically, the thread 2 in the example below will start a counter and if the counter reaches 5 it sets the global variable endThreadOne to 1 which thread 1  will also check it periodically. Once it founds out that the flag is set to 1, it will exit itself. Since it is python's thread library, thread is represented by function instead of object and exiting the function will effectively ends the thread.

Credits for giving fast and furious information to get me started in no time to following:

This gives very good basic intro-s on basics of python thread:
http://www.techbeamers.com/python-multithreading-concepts/

Good example which I extended as described above: 
https://www.tutorialspoint.com/python/python_multithreading.htm

#!/virtualenv/bin/python

import thread
import time
import os

global endThreadOne
print "setting end thread to No=0: "
endThreadOne = 0

TTHREAD_1_NAME = "Thread-1: "
TTHREAD_2_NAME = "Thread-2: "

# Define a function for the thread

def printStatThread( threadName, delay):
    count = 0
    global endThreadOne

    print "\nendThreadOne stat: ", endThreadOne

    while count < 20:
        time.sleep(delay)
        count += 1
        print "    -", threadName, count

        if threadName == TTHREAD_2_NAME and count > 5 and endThreadOne == 0:
            print threadName, "Telling thread 1 to exit."
            endThreadOne = 1
        if threadName == "Thread-1: " and endThreadOne == 1:
            print threadName, "I was told to exit. I am exiting."
            return 0

    print threadName, "Timeout, I am exiting"


# Create two threads as follows

try:
    thread.start_new_thread( printStatThread, (TTHREAD_1_NAME, 2, ) )
    thread.start_new_thread( printStatThread, (TTHREAD_2_NAME, 4, ) )
except:
    print "Error: unable to start thread"

#os.system("clear")
counter = 0


while 1:
    time.sleep(5)

    if counter > 10:
        print "Exiting."
        break

    counter += 1
    print "1. loop " + str(counter)
    continue

print "done"


Resulting output appears to do what it is intended to do. Both thread starts and started looping using count and prints its thread name along with counter. Once thread 2 detects counter is reached to 5, it sets the global flag which thread 1 uses it to end itself.

endThreadOne stat:  0
endThreadOne stat:
 0
    - Thread-1:  1
    - Thread-2:  1
    - Thread-1:  2
1. loop 1
    - Thread-1:  3
    - Thread-2:  2
    - Thread-1:  4
1. loop 2
    - Thread-1:  5
    - Thread-2:  3
    - Thread-1:  6
    - Thread-1:  7
1. loop 3
    - Thread-2:  4
    - Thread-1:  8
    - Thread-1:  9
1. loop 4
    - Thread-2:  5
    - Thread-1:  10
    - Thread-1:  11
    - Thread-2:  6
Thread-2:  Telling thread 1 to exit.
    - Thread-1:  12
Thread-1:  I was told to exit. I am exiting.
1. loop 5
    - Thread-2:  7
1. loop 6
    - Thread-2:  8
1. loop 7
    - Thread-2:  9
1. loop 8
    - Thread-2:  10
    - Thread-2:  11
1. loop 9
    - Thread-2:  12
1. loop 10
    - Thread-2:  13
1. loop 11
    - Thread-2:  14
Exiting.
done

Source: python thread management

dlots

Thank you
I have been wanting to try and figure out multiple threads!!

martingever

The Python threading module uses threads instead of processes. Threads run in the same unique memory heap. Whereas Processes run in separate memory heaps. This, makes sharing information harder with processes and object instances. One problem arises because threads use the same memory heap, multiple threads can write to the same location in the memory heap which is why the global interpreter lock(GIL) in CPython was created as a mutex to prevent it from happening.

The multithreading library is lightweight, shares memory, responsible for responsive UI and is used well for I/O bound applications. Check this simple example of...multithreaded socket programming