How to solve this error? i want to pass the values from get_robotxya() and get_ballxya()
and use it in a loop but it seems that it will crash after awhile how do i fix this? i want to get the values whithout it crashing out of the while loop
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])
return
def get_ballxy():
update_coordinates()
ballx = int(COORDINATES[8])
bally = int(COORDINATES[9])
return ballx,bally
def get_robotxya():
update_coordinates()
robotx = int(COORDINATES[12])
roboty = int(COORDINATES[13])
angle = int(COORDINATES[14])
return robotx,roboty,angle
def print_ballxy(bx,by):
print bx
print by
def print_robotxya(rx,ry,a):
print rx
print ry
print a
def activate():
bx,by = get_ballxy()
rx,ry,a = get_robotxya()
print_ballxy(bx,by)
print_robotxya(rx,ry,a)
Thread(target=update_coordinates).start()
while True:
activate()
this is the error i get:

I think you’ll find that, because you’re continuously creating new connections without closing them down, you’ll eventually run out of resources.
That may be a local limitation or it may be the server end getting fed up with too many connections from a single IP.
Regardless, either create one connection and use it over and over, or shut down connections when you’re done with them.
One possible way of doing this is to
connect()within the main code before callingactivate():Then remove the initial
connect()from the start of theupdate_coordinates()function since that’s the bit that does a new connection every time you try to update the coordinates.If the session goes down for some reason, the
exceptbit will re-create it and try again.That should hopefully alleviate your resource problem quite a bit.