我在使用线程锁时无法关闭客户端

时间:2017-07-30 13:07:31

标签: python multithreading python-sockets

这是客户端代码,它作为一个客户端工作得很好但是当我告诉它时显示错误

这是代码

import socket
import threading


tLock = threading.Lock()
shutdown = False

def receving(name, sock):
    while not shutdown:
        try:
            tLock.acquire()
            while True:
                data, addr = sock.recvfrom(1024)
                print (str(data))
        except:
            pass
        finally:
            tLock.release()

host = '127.0.0.1'
port = 0

server = ('127.0.0.1',5000)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
s.setblocking(0)

rT = threading.Thread(target=receving, args=("RecvThread",s))
rT.start()

alias = input("Name: ")
message = input(alias + "-> ")
while message != 'q':
    if message != '':
        s.sendto(bytes(alias + ": " + message,"utf8"), server)
    tLock.acquire()
    message = input(alias + "-> ")
    tLock.release()

shudown = True
rT.join()
s.close()

当我在shell中插入 q 时,它会停止shell,但客户端正在运行但不知道为什么

这是我按ctrl + c

时显示的内容
Traceback (most recent call last):
  File "client2.py", line 42, in <module>
    rT.join()
  File "/usr/lib/python3.5/threading.py", line 1054, in join
    self._wait_for_tstate_lock()
  File "/usr/lib/python3.5/threading.py", line 1070, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt
^CException ignored in: <module 'threading' from '/usr/lib/python3.5/threading.py'>
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 1288, in _shutdown
    t.join()
  File "/usr/lib/python3.5/threading.py", line 1054, in join
    self._wait_for_tstate_lock()
  File "/usr/lib/python3.5/threading.py", line 1070, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt

如何解决这个问题的一些问题

0 个答案:

没有答案