如何保持关闭子线程而不关闭stdin文件描述符?

2024-04-20 08:10:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了这段代码来澄清我的问题。。。我不断得到ValueError:对关闭的文件执行I/O操作。你知道吗

没有从stdin读取的子线程。循环工作得很好,直到我开始一个子线程。。。有人能告诉我如何防止文件描述符关闭吗?你知道吗

import threading
from threadtest2 import Threadtest
import termios, sys, tty
import time

def getchar():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch


tt2 = Threadtest()
stop = threading.Event()

t1 = threading.Thread(target=tt2.thread1, args=[stop, ])
t2 = threading.Thread(target=tt2.thread2, args=[stop, ])

try:
    while 1:
        while not stop.isSet():
            try:
                c = getchar()
            except IOError: pass
            if c == "q":
                stop.set()
            if c == "x":
                stop.set()
                exit()
            if c == "1":
                print "starting t1"
                t1.start()
            if c == "2":
                print "starting t2"
                t2.start()
        while len(threading.enumerate()) > 1:
            print 'waiting for ' + str(len(threading.enumerate()) - 1) + ' threads to close\r'
            time.sleep(1)
        stop.clear()
        print "stop has been triggered and reset... restart"
finally:
    print "done!"

有几个其他的线程(请原谅双关语)触及这个,但我没有找到一个直接解决它,并已挥舞了一段时间。你知道吗

仅供参考,孩子们只是等待停止设置和睡眠。。。你知道吗


Tags: importifstdinsystt2线程stopt1
1条回答
网友
1楼 · 发布于 2024-04-20 08:10:34

我对你的代码做了一些小的修改,使之独立运行。以下内容不会为我在Linux机器上生成错误。你还看到它的错误吗?如果是这样的话,我很乐意改进答案-请提供一些关于如何运行代码的更多细节,例如正在使用的操作系统。你知道吗

import threading
import termios, sys, tty
import time

def getchar():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch

class Threadtest:
    def thread1(self, stop):
        stop.wait()
        print "returning from thread1"
    def thread2(self, stop):
        stop.wait()
        print "returning from thread2"

tt2 = Threadtest()
stop = threading.Event()

try:
    while 1:
        t1 = threading.Thread(target=tt2.thread1, args=[stop, ])
        t2 = threading.Thread(target=tt2.thread2, args=[stop, ])

        while not stop.isSet():
            try:
                c = getchar()
            except IOError: pass
            if c == "q":
                stop.set()
            if c == "x":
                stop.set()
                sys.exit()
            if c == "1":
                print "starting t1"
                t1.start()
            if c == "2":
                print "starting t2"
                t2.start()
        print "waiting for {} threads to close".format(threading.active_count() - 1)
        for t in [t1, t2]:
            t.join()
        stop.clear()
        print "stop has been triggered and reset... restart"
finally:
    print "done!"

相关问题 更多 >