仅在远程主机上的Python引导程序线程错误

时间:2014-06-01 15:32:01

标签: python multithreading sockets

我一直在运行包含三个线程的脚本;

主线程处理IRC消息,第二个是包含套接字服务器的线程,当它作为客户端连接时,它将字符串从IRC发送到闪存显示,第三个是发送策略文件的套接字服务器。客户端连接时的闪存要求。

python文件在“localhost”上运行正常,但是当我将它托管在我的linode上时,它只收到第一个字符串并发送闪存上显示的字符串响应,然后崩溃并显示以下错误消息。

以下错误:

Traceback (most recent call last):
  File "/opt/python3.3/lib/python3.3/threading.py", line 616, in _bootstrap
    self._bootstrap_inner()
  File "/opt/python3.3/lib/python3.3/threading.py", line 682, in _bootstrap_inner
    self._stop()
TypeError: 'Event' object is not callable

这是我的套接字服务器代码:

class FlashSOCKET (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self._stop = threading.Event()
        self.stopWaitLoop = False
        self.conn = None
        self.addr = None
        self.HOST = None
        self.PORT = 7001
        self.s = None

    def run(self):
        global running, toSend, message
        print ("Starting " + self.name)
        while running:
            self.startListening()
            while running:
                if (messageDeque):
                    try:
                        print(self.conn.recv(1024))
                        self.conn.send(messageDeque.popleft().encode('utf8'))
                        print (messageDeque)
                        break
                    except:
                        pass # Nothing to do keep listening

            if (self.conn != None):
                self.close_connection()
        if (self.conn != None):
            self.close_connection()

        print ("Exiting " + self.name)

    def startListening(self):
        global running, message, toSend
        print ("starting to listen")
        for res in socket.getaddrinfo(self.HOST, self.PORT, socket.AF_UNSPEC,
                                      socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
            af, socktype, proto, canonname, sa = res
            try:
                self.s = socket.socket(af, socktype, proto)
                self.s.settimeout(0.5)
            except socket.error as msg:
                self.s = None
                continue
            try:
                self.s.bind(sa)
                self.s.listen(1)

            except socket.error as msg:
                print ("socket error")
                self.s.close()
                self.s = None
                continue
            break
        if self.s is None:
            print ('could not open socket')
            sys.exit(1)
        while(running):
            try:
                self.conn, self.addr = self.s.accept()
                time.sleep(1)
                break
            except:
                continue
        print ('Connected by', self.addr)

    def close_connection(self):
        self.conn.close()
        print ("connection closed")

Running是一个全局bool标志变量,可以让我在需要时停止程序。 messageDeque是一个deque缓冲区,当人们在IRC中讲话时,它会在主线程中填充字符串。

我认为,发送和消息是多余的代码。

1 个答案:

答案 0 :(得分:0)

  1. 尽量避免全局变量。在正常编程中,它们是粗略的,但在多线程编程中,它们更具误导性而不是有用。

  2. Event对象:执行if self._stop.is_set()与检查全局