繁体   English   中英

IRC客户端应用程序,如何在python中使用select()

[英]IRC client application, how to use select() in python

嗨,我正在研究从标准输入读取数据并将消息发送到无限循环的服务器的网络应用程序。 对于每个循环,我都有线程,该线程调用函数应执行的操作。 我需要能够捕获SIGINT并正确关闭两个线程。 我的程序卡在两个读取功能上。 我想我应该使用select()函数,但是我不知道如何。

def writing_mode():
    while 1:
        var = raw_input()
        # some options with what to do with the var

def listening_mode():
    readbuffer = ""

    while 1:
        readbuffer = readbuffer + s.recv(1024)
        temp = string.split(readbuffer, "\r\n")
        readbuffer = temp.pop()

        for line in temp:
            line = string.strip(line)
            prefix, command, rest, trailing = parse(line)

            # do something with the options u have

s=socket.socket()
try:
    s.connect((args.h, args.p))
except Exception, e:
    print("Something is wrong with %s:%d, Exception type is:\n%s" % (args.h, args.p, e))
    sys.exit()

s.send("NICK AAA\r\n")
s.send("USER AAA AAA AAA AAA\r\n")

thread.start_new_thread(writing_mode)
thread.start_new_thread(listenning_mode)
#main program waiting for signals

我已经通过select()进行非阻塞读取来解决此问题。

while condition:
    # s is socket to read from
    # for stdin use sys.stdin instead
    ready = select.select([s], [], [], 0)
    if ready[0]:
        #u can start read

选择功能仅在有要读取的内容时才继续读取功能。 否则,它将进入while循环,因此条件可以随时将其停止。

暂无
暂无

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

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