Answers for "multi thread python"

5

create new thread python

from threading import Thread
from time import sleep

def threaded_function(arg):
    for i in range(arg):
        print("running")
        sleep(1)


if __name__ == "__main__":
    thread = Thread(target = threaded_function, args = (10, ))
    thread.start()
    thread.join()
    print("thread finished...exiting")
Posted by: Guest on June-27-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
0

how to multi thread python

from Threading import Thread

def thread_fn():
	# do stuff here. 
    
num_threads = 10
for i in range(num_threads):
	t = Thread(target=thread_fn, args=[]) # args[] contains function arguments
    t.start()                             # thread is now running!
    threads.append(t)                     # hold onto it for it later

for t in threads:
    t.join()                              # waits for each t to finish
Posted by: Guest on July-10-2021

Python Answers by Framework

Browse Popular Code Answers by Language