运行两个在循环中相互依赖的线程

时间:2012-09-27 11:51:27

标签: python multithreading

我需要知道如何编写一个运行两个线程的程序: 1.线程更新对象的位置(值) 2.一个线程,它根据第一个线程的值在其中运行两个方法 所有这一切都在循环中

这是我所拥有的当前代码,我想按照我的描述进行更改,而不是值是valueList的形式,而activate(valueList)是一个包含几个较小方法的方法。如果你不介意我会喜欢有一个例子来解决这个问题。谢谢

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('59.191.193.42',5555))


screenw = 0
screenh = 0
while 1:
    client_socket.send("loc\n")
    data = client_socket.recv(8192)
    valueList = data.split()

    if (not(valueList[-1] == "eom" and valueList[0] == "start")):
        #print "continuing.."
            continue

        if(screenw != int(valueList[2])):
            screenw = int(valueList[2])
            screenh = int(valueList[3])

    activate(valueList)

1 个答案:

答案 0 :(得分:2)

正如其他人所指出的,它看起来并不像线程实际上会解决你的问题。特别是因为Python代码受Global Interpreter Lock的约束,这基本上意味着只有当您的代码受IO限制(从磁盘读取大文件,等待慢速网络连接等)时,引导才会对您有所帮助。如果您的程序受CPU限制并且您真的想要利用并行处理,multiprocessing是可行的方法。通过多处理,您可以交换一些内存开销和一点延迟(在创建进程时和进程间通信期间),以便利用多核CPU。

由于并行处理对您的程序有用,或者您只是好奇,我提供以下代码示例。免责声明,我没有试图导入这个模块,所以考虑它伪代码。

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 process
def update_proc(data_queue, update_queue, quit_flag):
    while not 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 process
def activate_proc(update_queue, quit_flag):
    while not 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 terminal
if __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_queue
    while True:
        client.sendall("loc\n")
        data = client.recv(8192)
        if not data:
            print "network connection closed by client"
            break
        data_queue.put(data)

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