简体   繁体   English

在一个循环python中崩溃

[英]crashing out in a while loop python

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? 我想传递来自get_robotxya()和get_ballxya()的值并在循环中使用它但似乎它会在一段时间后崩溃我如何解决这个问题? i want to get the values whithout it crashing out of the while loop 我希望得到的值不会从while循环中崩溃

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. 这可能是本地限制,也可能是服务器端厌倦了来自单个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 calling activate() : 一种可能的方法是在调用activate()之前在主代码中connect() activate()

connect()
update_coordinates()
while True:
    activate()

Then remove the initial connect() from the start of the update_coordinates() function since that's the bit that does a new connection every time you try to update the coordinates. 然后从update_coordinates()函数的开头删除初始connect() ,因为每次尝试更新坐标时都会执行新连接。

If the session goes down for some reason, the except bit will re-create it and try again. 如果会话由于某种原因而中断,则except位将重新创建并重试。

That should hopefully alleviate your resource problem quite a bit. 这应该有望缓解你的资源问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM