<jupyter_start><jupyter_text>At the start of this video  1. We were able to use threading in our Python code.<jupyter_code>from threading import Thread, Lock
thread_lock = Lock()
# Not a good practise to use global variables
my_global_string = "Hello World"

def add_prefix(prefix_to_add):
    # Adds suffix to global string
    
    global my_global_string
    
    # Acquire the lock over the data shared between threads
    thread_lock.acquire()
    
    # Perform operation on shared data
    my_global_string = prefix_to_add + " " + my_global_string
    
    # Release the lock
    thread_lock.release()
def add_suffix(suffix_to_add):
    # Adds suffix to global string
    
    global my_global_string
    
    # Acquire the lock over the data shared between threads
    thread_lock.acquire()
    
    # Perform operation on shared data
    my_global_string = my_global_string + " " + suffix_to_add 
    
    # Release the lock
    thread_lock.release()
def do_threading():
    
    thread_prefix = Thread(target=add_prefix, args=("YOLO",))
    thread_suffix = Thread(target=add_suffix, args=("BYE!!",))
    
    thread_prefix.start()
    thread_suffix.start()
    
    thread_prefix.join()
    thread_suffix.join()
    
    global my_global_string
    print("Final string is {}".format(my_global_string))
do_threading()<jupyter_output>Final string is YOLO Hello World BYE!!<jupyter_text>Re-entrant Lock<jupyter_code>from threading import RLock
my_re_entrant_lock = RLock()
my_re_entrant_lock.acquire()
my_global_string = "yolo swag"

# If this was a conventional Lock, then this would a blocking call for the thread
# Even though the same thread is trying to acess it again.
my_re_entrant_lock.acquire()   
my_global_string += " ok bye !"


my_re_entrant_lock.release()
my_re_entrant_lock.release()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07738419618528611, 'fraction_numerical': 0.0005449591280653951, 'mean_word_length': 3.896, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '63438', 'license': 'MIT', 'n_tokens_mistral': 601, 'n_tokens_neox': 591, 'n_words': 163, 'path': 'Section 2/notebooks/2. 2 Using Locks with Threads.ipynb', 'repo_name': 'PacktPublishing/High-Performance-Computing-with-Python-3.x', 'size': '1835'}