Python TCP服务器在一段时间后停止响应

时间:2016-06-07 01:52:56

标签: python raspberry-pi tcpsocket

所以我在我的RPI上编写了这个python代码来创建一个与我的crestron控制处理器对话的数字海报。出于某种原因,我的代码在一段时间后停止响应。我不知道为什么。 我不知道我的Pi是否会睡觉,但是如果有人能在我的代码中指出可能导致这种情况的任何内容,请告诉我。

编辑:我自己完成了自己的工作。非常感谢所有评论的人。

每次进行循环时我都会创建一个新的FBI进程,最后我用尽所有Pi的RAM,直到它停止,因为它没有任何剩余。我现在在加载修复它的图像后杀死FBI进程。感谢大家的帮助。

我的功能和设置材料:

import socket
import os
import thread
import sys
import urllib
from time import ctime
from PIL import Image
import time
import random
import logging

frameBufferCommand = "sudo fbi -a -T 1 -d /dev/fb0 -noverbose "
moviePosterLocation = "/home/pi/movieposters/"
openBlackImage = "/home/pi/movieposters/special/black.jpg"
killFrameBuffer = "sudo killall fbi"
turnOffScreen = "sudo /opt/vc/bin/tvservice -o"
turnOnScreen = "sudo /opt/vc/bin/tvservice -p"

def splitString(input): 
    stringList = input.split("/x00/") 
    return stringList

def displayRandomPoster():
    displayingPoster = False
    fileCount = next(os.walk(moviePosterLocation))[2] #get the number of available posters
    print('Current # Of Posters in Directory: ' + str(len(fileCount)))

    attemptNumber = 0

    while not displayingPoster:
        posterToDisplay = random.randint(0, len(fileCount))
        print('Trying To Display A Random Poster')
        attemptNumber += 1

        try:
            image = Image.open(moviePosterLocation + fileCount[posterToDisplay]) #open the current image
            width, height = image.size
        except IndexError as msg:
            print("encountered an IndexError while opening an image")
            width = 0
            height = 0      
        except IOError as msg:
            print(msg)
        try:
            conn.send(msg)
        except socket.error as msg:
            print("Socket Error")


    if width > height:
        if attemptNumber > 5:
            print("Too Many Attempts, Stopping")
            break
        else:
            print("We Think This Isnt A Poster, Trying Again")               
            continue    
    else:
        try:
            conn.send("  Displaying Poster: " + str(fileCount[posterToDisplay]))
        except socket.error as msg:
            print msg                
        os.system(frameBufferCommand + moviePosterLocation + fileCount[posterToDisplay])
        displayingPoster = True
        return

正在侦听的线程和主循环:

#-- CLIENT THREAD --

def client(conn):
    try:
        conn.send("Connected With Raspberry Pi")
        os.system(frameBufferCommand + openBlackImage)
    except socket.Error as msg:
        print(msg)

print(str(bindSuccess))

while 1:    
    dataReceived = conn.recv(1024)
    reply = "OK.. "
    conn.send(reply + dataReceived)
    dataList = splitString(dataReceived)
    print(dataList)
    if dataList[6] == "": 
        print("dataList[6] is empty, displaying a random poster")
        displayRandomPoster()
    else: 
        try:    
            moviePosterURL = dataList[6]            
            splitPosterURL = moviePosterURL.split("%")            
            moviePosterFilename = splitPosterURL[7]                
            urllib.urlretrieve(moviePosterURL, moviePosterLocation + moviePosterFilename)                
            conn.send("XBMC Movie Poster Downloading")
            conn.send("Movie Poster saved as: " + moviePosterLocation + moviePosterFilename)
            print("Movie Poster saved as: " + moviePosterLocation + moviePosterFilename)                            
        except socket.error:
            print("Encountered Socket Error While Downloading Poster - Line 80")            
        try:
            os.system(frameBufferCommand + moviePosterLocation + moviePosterFilename)
            print('Opening Downloaded Poster')
        except OSError as msg:
            print(str(msg[0]) + str(msg[1]))
    if not dataReceived:
        break
    time.sleep(1)


#-- MAIN FUNCTION --

host = ''
communicationPort = 5005

bindSuccess = False

crestronSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print("Socket Created Successfully")

try:
    crestronSocket.bind((host, communicationPort))
    bindSuccess = True
except socket.Error as msg: 
    bindSuccess = False
    print("Socket Bind Error" + msg)
    sys.exit()

crestronSocket.listen(1)
print("Crestron Socket Listening")

while bindSuccess == True:
    #wait to accept a connection - blocking call
    conn, addr = crestronSocket.accept()
    print('Connected with Client: ' + addr[0] + ':' + str(addr[1]))      
    thread.start_new_thread(client , (conn,))
crestronSocket.close()

1 个答案:

答案 0 :(得分:0)

每次进行循环时我都会创建一个新的FBI进程,最后我用尽所有Pi的RAM,直到它停止,因为它没有任何剩余。我现在在加载修复它的图像后杀死FBI进程。感谢大家的帮助。

我还想出了一些能让它更好/更无缝地运作的其他东西。