简体   繁体   English

Python线程未处理的异常

[英]Python Threading Unhandled Exception

Questions 问题

  1. What is the reason for the exception? 异常的原因是什么?

  2. Did the client cause any errors? 客户端是否导致任何错误?

  3. If at all possible, please explain other errors. 如果可能的话,请解释其他错误。

Background 背景

I am creating a Python GUI socket Server. 我正在创建一个Python GUI套接字服务器。 When a client connects to my server, the GUI window will open (I am still working on this). 当客户端连接到我的服务器时,GUI窗口将打开(我仍在处理此问题)。 But, when a client does connect, I get an error: 但是,当客户端连接时,我收到一个错误:

Unhandled exception in thread started by <function clientthread at 0x10246c230>

Since the actual script is rather long, I have provided a pastebin link. 由于实际脚本相当长,我提供了一个pastebin链接。

Here is the thread code. 这是线程代码。 s is the name of my socket object. s是我的socket对象的名称。

def clientthread(s):

    #Sending message to connected client
    #This only takes strings (words
    s.send("Welcome to the server. Type something and hit enter\n")

    #loop so that function does not terminate and the thread does not end
    while True:

        #Receiving from client
        data = s.recv(1024)
        if not data:
            break
        s.sendall(data)
        print data
    s.close()

Traceback 追溯

Thanks for the suggestion Morten . 感谢Morten的建议。 Here is the traceback. 这是追溯。

Socket Created
Socket Bind Complete
Socket now listening
Connected
Traceback (most recent call last):
  File "/Users/BigKids/Desktop/Coding/Python 2/Sockets/Function/Server GUI Alpha Function.py", line 80, in clientthread
    s.send("Welcome to the server. Type something and hit enter\n")
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor

Personally, I believe that many errors are due to the GUI. 就个人而言,我认为很多错误都是由GUI造成的。

Thanks! 谢谢!

For one, you could catch the exception, print it and see what it is :) 首先,您可以捕获异常,打印它,看看它是什么:)

Do this, for instance by surrounding it all with a try/except clause and printing whatever exception occurs. 这样做,例如通过try / except子句包围它并打印出现的任何异常。

def clientthread(s):
    try:
        #Sending message to connected client
        #This only takes strings (words
        s.send("Welcome to the server. Type something and hit enter\n")

        #loop so that function does not terminate and the thread does not end
        while True:

            #Receiving from client
            data = s.recv(1024)
            if not data:
                break
            s.sendall(data)
            print data
        s.close()
    except Exception:
        import traceback
        print traceback.format_exc()

I'm guessing the reason for this is client disconnect. 我猜这是客户端断开的原因。 This will cause an exception and you should handle it appropriately. 这将导致异常,您应该适当地处理它。 If a client can disconnect in many ways. 如果客户端可以通过多种方式断开连接。 By telling you, by timing out, by dropping the connection while you're trying to send something etc. All these scenarios are plausible exception cases, and you should test for them and handle them. 告诉你,通过超时,在你尝试发送东西时删除连接等等。所有这些情况都是合理的异常情况,你应该测试它们并处理它们。 Hopefully this will help you move on, if not, please comment :) 希望这会帮助你继续前进,如果没有,请评论:)

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

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