Skip to content Skip to sidebar Skip to footer

Running Two Threads Which Are Dependent On Each Other In A Loop

I need to know how to write a program that runs two threads: 1. a thread the updates the position(values) of an object 2. a thread that runs two methods in it based on the values

Solution 1:

As others have pointed out, It doesn't look like threading is actually going to solve your problem. Especially because Python code is subject to the Global Interpreter Lock, which basically means theading will only help you if your code is IO-bound (reading large files from disk, waiting on a slow network connection, etc). If your program is CPU-bound and you really want to take advantage of parallel processing, multiprocessing is the way to go. With multiprocessing, you trade some memory overhead and a little latency (when the process is created and during inter-process communication) for the ability to take advantage of multi-core CPUs.

Just on the off chance that parallel processing does turn out to be useful for your program, or you're just curious, I present the following code sample. Disclaimer, I haven't so much as tried to import this module, so consider it pseudocode.

import socket
from multiprocessing import Process, Queue, Value
from ctypes import c_bool

HOST = '198.51.100.0'
PORT = 8080# This function will be run in a child processdefupdate_proc(data_queue, update_queue, quit_flag):
    whilenot quit_flag.value:
        data = data_queue.get()
        # do something with the data...
        update_queue.put(data)
    print"Closing child update process"# This function will be run in a child processdefactivate_proc(update_queue, quit_flag):
    whilenot quit_flag.value:
        data = update_queue.get()
        # do something with the data...print"Closing child activate process"# main process begins execution here, if module is run from the terminalif __name__ == "__main__":
    # Connect to remote host over TCP
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((HOST,PORT))

    # Set up a Queue to pass data to the update process, and another one# for the two children to communicate
    data_queue = Queue()
    update_queue = Queue()

    # The quit_flag Value is a *very* primitive way to signal the child # processes to quit. I'm sure there are better ways to do this, but I'm # tired and can't think of any right now.
    quit_flag = Value(c_bool, False)

    # Create two child processes, pass a reference to the Queue to each
    update = Process(target=update_proc, args=(data_queue, update_queue, quit_flag))
    activate = Process(target=activate_proc, args=(update_queue, quit_flag))

    update.start()
    activate.start()

    # Read data from the TCP socket, push it onto the data_queuewhileTrue:
        client.sendall("loc\n")
        data = client.recv(8192)
        ifnot data:
            print"network connection closed by client"break
        data_queue.put(data)

    # Join with child processes before closingprint"All done, closing child processes"
    update.join()
    activate.join()

Post a Comment for "Running Two Threads Which Are Dependent On Each Other In A Loop"