Answers for "thread await 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
1

python threading async

import queue

def task(name, work_queue):
    if work_queue.empty():
        print(f"Task {name} nothing to do")
    else:
        while not work_queue.empty():
            count = work_queue.get()
            total = 0
            print(f"Task {name} running")
            for x in range(count):
                total += 1
            print(f"Task {name} total: {total}")

def main():
    """
    This is the main entry point for the program
    """
    # Create the queue of work
    work_queue = queue.Queue()

    # Put some work in the queue
    for work in [15, 10, 5, 2]:
        work_queue.put(work)

    # Create some synchronous tasks
    tasks = [(task, "One", work_queue), (task, "Two", work_queue)]

    # Run the tasks
    for t, n, q in tasks:
        t(n, q)

if __name__ == "__main__":
    main()
Posted by: Guest on March-06-2021

Python Answers by Framework

Browse Popular Code Answers by Language