线程不工作如何解决这个问题?

时间:2012-10-05 01:07:01

标签: python sockets

当我想访问int(COORDINATES 2)时,这不起作用。它不起作用。我该如何解决?我正在尝试访问现在已成为全球范围的COODINATES

import socket
import os,sys
import time
from threading import Thread

HOST = '59.191.193.59'
PORT = 5555

COORDINATES = []

def connect():   
    globals()['client_socket'] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((HOST,PORT))

def update_coordinates():
    connect()
    screen_width = 0
    screen_height = 0
    while True:
        try:
            client_socket.send("loc\n")
            data = client_socket.recv(8192)
        except:
            connect();
            continue;

        globals()['COORDINATES'] = data.split()

        if(not(COORDINATES[-1] == "eom" and COORDINATES[0] == "start")):
            continue

        if (screen_width != int(COORDINATES[2])):
        screen_width = int(COORDINATES[2])
                screen_height = int(COORDINATES[3])   

Thread(target=update_coordinates).start()

while True:
    print 'ball x' , int(COORDINATES[2])

这是我得到的错误enter image description here enter image description here

如果我将我的代码更改为:

    import socket
import os,sys
import time
from threading import Thread

HOST = '59.191.193.59'
PORT = 5555

COORDINATES = []

def connect():   
    globals()['client_socket'] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((HOST,PORT))

def update_coordinates():
    connect()
    screen_width = 0
    screen_height = 0
    while True:
        try:
            client_socket.send("loc\n")
            data = client_socket.recv(8192)
        except:
            connect();
            continue;

        globals()['COORDINATES'] = data.split()

        if(not(COORDINATES[-1] == "eom" and COORDINATES[0] == "start")):
            continue

        if (screen_width != int(COORDINATES[2])):
        screen_width = int(COORDINATES[2])
                screen_height = int(COORDINATES[3]) 

    print int(COORDINATES[8])  

Thread(target=update_coordinates).start()

这就是我得到的:

enter image description here

1 个答案:

答案 0 :(得分:1)

这一行:

Thread(target=update_coordinates()).start()

应阅读:

Thread(target=update_coordinates).start()

您已将函数的结果作为target的值提供,而不是提供函数本身。