Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

multi threading in python for 2 different functions with return

from queue import Queue
from threading import Thread
import time

def f1(q, x):
    # Sleep function added to compare execution times.
    time.sleep(5)
    # Instead of returning the result we put it in shared queue.
    q.put(x * 2)

def f2(q, x):
    time.sleep(5)
    q.put(x ^ 2)

if __name__ == '__main__':
    x1 = 10
    x2 = 20
    result_queue = Queue()

    # We create two threads and pass shared queue to both of them.
    t1 = Thread(target=f1, args=(result_queue, x1))
    t2 = Thread(target=f2, args=(result_queue, x2))

    # Starting threads...
    print("Start: %s" % time.ctime())
    t1.start()
    t2.start()

    # Waiting for threads to finish execution...
    t1.join()
    t2.join()
    print("End:   %s" % time.ctime())

    # After threads are done, we can read results from the queue.
    while not result_queue.empty():
        result = result_queue.get()
        print(result)
Comment

PREVIOUS NEXT
Code Example
Python :: how to find length of list python 
Python :: decimal to binary python 
Python :: wap in python to check a number is odd or even 
Python :: python terminal progress bar 
Python :: python string iterate 3 characters at a time 
Python :: Aggregate on the entire DataFrame without group 
Python :: python to postgresql 
Python :: sort values within groups pandas dataframe 
Python :: python argparser flags 
Python :: confusion matrix 
Python :: import module python same directory 
Python :: python char at 
Python :: remove empty string from list python single line 
Python :: reverse python dictionary 
Python :: python sys 
Python :: groupby and list 
Python :: how to negate a boolean python 
Python :: index duplicates python 
Python :: list to dataframe pyspark 
Python :: ip address finder script python 
Python :: python in stack implementation 
Python :: dataframe python 
Python :: tensorflow evaluation metrics 
Python :: edit path variable using python 
Python :: raw string python 
Python :: find the place of element in list python 
Python :: python 2 print in same line 
Python :: updateview 
Python :: how to change entry in a row based on another columns entry python 
Python :: check if key exists in sesison python 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =