我如何从关闭标准输入文件描述符中保持关闭子线程?

时间:2018-08-12 15:18:58

标签: python multithreading stdin

我写了这段代码来澄清我的问题...我不断收到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!"

还有其他一些线程(对双关语很感动),但是我还没有找到一个直接解决这个问题的方法,并且已经花了一段时间了。

仅供参考,子线程仅等待停止设置并进入睡眠状态

1 个答案:

答案 0 :(得分:1)

我对您的代码做了一些小的更改以独立运行它。以下内容在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!"