使用input的Python中的EOFError

问题描述

我想创建一个脚本,该脚本将检查学生提交的代码是否按预期工作。代码经常涉及到让用户输入代码。我的想法是运行两个线程,一个运行代码,另一个运行模拟输入,然后检查结果是否匹配。
当前的问题是,在线程内部多次运行输入会导致此错误:

EOFError:读取一行时出现EOF。

此错误的原因是什么,如何解决?在线程外运行相同的代码时,不会导致错误。

import pynput
import threading
import time
from pynput.keyboard import Key,Controller
from queue import Queue

answer_queue = Queue()

def control_thread(answer_queue):
    time.sleep(1)
    keyboard = Controller()
    
    keyboard.press('5')
    keyboard.release('5')
    time.sleep(0.1)
    keyboard.press(Key.enter)
    keyboard.release(Key.enter)
    try:
        message = answer_queue.get(timeout = 0.1)
        split_message = message.split(";") 
        msgQueue.task_done()
        print(message)
    except:
        pass


def solution_thread(answer_queue):
    length = int(input())
    width = int(input())
    
    answer_queue.put(length)#,width)
    
solution = threading.Thread(target = solution_thread,args = (answer_queue,))
solution.start()
control = threading.Thread(target = control_thread,))
control.start()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...