python程序冻结可能是因为split()

时间:2016-11-12 11:54:37

标签: python raspberry-pi freeze

我有一个与python程序通信的java程序。一切都运行正常,但经过一段时间后,我得不到python程序的输出。然后我添加了一个日志来查看代码停止的位置。日志中的最后一个条目显示它正在拆分输入。但我不知道问题是否也可能出现在“推”的第二个帖子中

程序获得16位数字,用于控制输出的行为(通过移位寄存器)

这是我的代码:

import socket
import sys
import StringIO
import time
import RPi.GPIO as GPIO
from threading import Thread
GPIO.setwarnings(False)

pdata = 16
pclock = 20
pshift = 21

GPIO.setmode(GPIO.BCM)
GPIO.setup(pdata, GPIO.OUT)
GPIO.setup(pclock, GPIO.OUT)
GPIO.setup(pshift, GPIO.OUT)

threadStop = False
dataIn = "0000000000000000"
working = True

tmp = ""
msg = ""

def log(logtext):
    f = open('outslog', 'a')
    f.write(logtext + '\n')

def push(id, stop):
    counter = 0
    counter2 = 0
    state = 0
    state2 = 0
    while(True):
        if stop():
            break
        tmp = ""
        data = dataIn
        counteri = 0
        for c in data:
            tmp += c
            counteri += 1
            if (int(c) == 0):
                add(c)
            elif(int(c) == 1):
                add(c)
            elif(int(c) == 2):   
                add(state)
            elif(int(c) == 3):
                add(state2)
        if(counter == 4):
            counter = 0
            if(state == 0):
                state = 1
            elif(state == 1):
                state = 0
        if(counter2 == 2):
            counter2 = 0
            if(state2 == 0):
                state2 = 1
            elif(state2 == 1):
                state2 = 0
        print "outputting(" + str(counteri) + ") + " + tmp
        #log("outputting(" + str(counteri) + ") + " + tmp)
        shift()
        time.sleep(0.1)
        counter += 1
        counter2 += 1

def add(value):
    if(int(value) == 1):
        GPIO.output(pdata, False)#
    else:
        GPIO.output(pdata, True)#
    #time.sleep(0.02)
    tick()
    GPIO.output(pdata, True)#

def tick():
    GPIO.output(pclock, True)
    #time.sleep(0.02)
    GPIO.output(pclock, False)

def shift():
    print "shift"
    GPIO.output(pshift, True)
    #time.sleep(0.05)
    GPIO.output(pshift, False)

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('127.0.0.1', 765)
print 'connecting to ' + str(server_address) + "..."
sock.connect(server_address)
print 'connected.'
log("connected.")

t = Thread(target=push, args=(1, lambda: threadStop))
t.start()

while(working):
    print "waiting for input..."
    buffering = True
    bffr = ""
    #buff = StringIO.StringIO(2048)
    while(True):
        try:
            log("recieving...")
            tmpdata = sock.recv(1024)
            log("tmpdata recieved: <" + tmpdata + ">")
            #buff.write(tmpdata)
            bffr += tmpdata
            log("now testing...")
            if '/' in bffr:
                log("slash in bffr. break.")
                 break
           else:
                    log("no slash in <" + bffr + ">")
            for c in bffr:
                if c == '/':
                    break
        except KeyboardInterrupt:
            log("KEYBOARD INTERRUPT")
            threadStop = True
            working = False
            break
    bffr += "8"
    log("now splitting: <" + bffr + ">")
    try:
        msg = bffr.rsplit('/')[0]
    except Exception as e:
        log("Error: " + str(e))
    print "recieved: " + msg
    log("recieved: " + msg)
    dataIn = msg
log("aborted. bffr: " + bffr)

日志中的最后一项是“现在拆分:&lt; 0101101000111130/8&gt;”

0 个答案:

没有答案