2012-09-28 37 views
0

不斷得到指數超出範圍的錯誤,當我運行這段代碼我如何解決這個問題呢?這些數據包含幾個值,這些值將通過服務器傳遞給valueList,我想使用這些數據中的部分值。例如,int(valueList [8])包含球的x座標。我該如何解決這個問題?列表索引範圍錯誤

import os,sys 
import pygame 
import socket 
import time 
from multiprocessing import Process, Value 
from ctypes import c_bool 

Host = '59.191.193.45' 
Port = 5555 

def updatePosition(valueList, update_valueList, quit_flag): 
    while not quit_flag.value: 
     print 'Ball x', int(valueList[8]) 
     print 'Ball y', int(valueList[9]) 
     update_valueList = valueList[:] 
    print 'Closing child update process' 

def activateRobot(update_valueList, quit_flag): 
    while not quit_flag.value: 
     print 'Ball x', int(valueList[8]) 
     print 'Ball y', int(valueList[9]) 
     print 'turn to' 
     print 'move to', int(valueList[8]) 
     print 'move to', int(valueList[9]) 
    print 'Closing child activate robot process' 

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
client.connect((Host,Port)) 

valueList = [] 
update_valueList = [] 
quit_flag = Value(c_bool,False) 

update = Process(target = updatePosition, args=(valueList, update_valueList,quit_flag)) 
activate = Process(target = activateRobot, args=(update_valueList,quit_flag)) 

update.start() 
activate.start() 

while True: 
    client.sendall("loc\n") 
    data = client.recv(8192) 
    if not data: 
     print 'network connection close by client' 
     break 
    valueList = data.split() 

print 'All done, closing child process' 
update.join() 
activate,join() 
+4

有什麼錯誤信息(行號)?另外,你的最後一行應該是'activate.join()',而不是'activate join()'。 – nneonneo

+1

明顯的步調試這是登錄'valueList'(或至少'LEN(值列表)'如果你希望整個事情太大,也太可怕了閱讀)在'activateRobot'的'while'循環中。 – abarnert

+0

等一下...你已經有了一個全局變量'valueList'在主過程。你沒有做任何事情來分享它(例如'multiprocessing.Array')。所以子進程在啓動時會得到一個空列表的副本,並且它永遠不會在子進程中被編輯。所以它總是'[]'。我錯過了什麼嗎? – abarnert

回答

3

data = client.recv(8192) 

接收高達 8192字節。你可能會得到部分線條。該行不必長於8192字節纔會發生。您只需閱讀到目前爲止已經到達的網絡,這可能不是整條線路。

相關問題