Answers for "how multithreading works in python"

4

threading python

import threading
import time

def thread_function(name):
     print(f"Thread {name}: starting")
     time.sleep(2)
     print(f"Thread {name}: finishing")
 
my_thread = threading.Thread(target=thread_function, args=(1,))
my_thread.start()
time.sleep(1)
my_second_thread = threading.Thread(target=thread_function, args=(2,))
my_second_thread.start()
my_second_thread.join() # Wait until thread finishes to exit
Posted by: Guest on October-15-2020
5

multithreading in python

from multiprocessing.pool import ThreadPool

def stringFunction(value):
    my_str = 3 + value
    return my_str


def stringFunctio(value):
    my_str = 33 + value
    return my_str



pool = ThreadPool(processes=1)

    
thread1 = pool.apply_async(stringFunction,(8,))
thread2 = pool.apply_async(stringFunctio,(8,))

return_val = thread1.get()
return_val1 = thread2.get()
Posted by: Guest on February-23-2021
4

python 2.7 multithreading

from multiprocessing.pool import ThreadPool as Pool

pool_size = 10
pool = Pool(pool_size)

results = []

for region, directory_ids in direct_dict.iteritems():
    for dir in directory_ids:
        result = pool.apply_async(describe_with_directory_workspaces,
                                  (region, dir, username))
        results.append(result)

for result in results:
    code, content = result.get()
    if code == 0:
        # ...
Posted by: Guest on February-10-2021

Python Answers by Framework

Browse Popular Code Answers by Language