如何在Python中的子进程获取键盘输入
场景:在Python中使用multiprocessing模块的Process创建子进程,试图在子进程中获取键盘输入。
使用input()
在子进程中使用input()会弹出报错信息:EOFError: EOF when reading a line。
代码示例
from multiprocessing import Process
import sys
def test_input():
info = input()
print("start print info!")
print(info)
if __name__ == "__main__":
print("start progress!")
Process(target=test_input).start()
结果输出
start progress!
Process Process-1:
Traceback (most recent call last):
File "D:\software\Python\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
File "D:\software\Python\lib\multiprocessing\process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "D:\text_project\python\验收2\test.py", line 5, in test_input
info = input()
EOFError: EOF when reading a line
Process finished with exit code 0
使用sys.stdin.readline()
在子进程中使用sys.stdin.readline(),发现并不会等待键盘输入。
代码示例
import sys
from multiprocessing import Process
def test_input():
info = sys.stdin.readline()
print("start print info!")
print(info)
if __name__ == "__main__":
print("start progress!")
Process(target=test_input).start()
结果输出
start progress!
start print info!
Process finished with exit code 0
使用fn=sys.stdin.fileno()
在主进程中敲写代码fn=sys.stdin.fileno(),然后将获取到的文件描述符fn传入子进程,子进程敲写代码sys.stdin = os.fdopen(fn),然后就可以正常使用sys.stdin.readline()获取键盘输入了。
代码示例
import os
import sys
from multiprocessing import Process
def test_input(fn):
sys.stdin = os.fdopen(fn)
info = sys.stdin.readline()
print("start print info!")
print(info)
if __name__ == "__main__":
print("start progress!")
fn = sys.stdin.fileno()
Process(target=test_input, args=(fn, )).start()
结果输出
start progress!
this is my input.
start print info!
this is my input.
Process finished with exit code 0
如何在Python中的子进程获取键盘输入的更多相关文章
- 如何在Python中从零开始实现随机森林
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 决策树可能会受到高度变异的影响,使得结果对所使用的特定测试数据而言变得脆弱. 根据您的测试数据样本构建多个模型(称为套袋)可以减少这种差异,但是 ...
- 如何在Python中使用Linux epoll
如何在Python中使用Linux epoll 内容 介绍 阻塞套接字编程示例 异步套接字和Linux epoll的好处 epoll的异步套接字编程示例 性能考量 源代码 介绍 从2.6版开始,Pyt ...
- 面试官问我:如何在 Python 中解析和修改 XML
摘要:我们经常需要解析用不同语言编写的数据.Python提供了许多库来解析或拆分用其他语言编写的数据.在此 Python XML 解析器教程中,您将学习如何使用 Python 解析 XML. 本文分享 ...
- Java编程中获取键盘输入实现方法及注意事项
Java编程中获取键盘输入实现方法及注意事项 1. 键盘输入一个数组 package com.wen201807.sort; import java.util.Scanner; public clas ...
- 在Delphi中使用键盘勾子获取键盘输入(译--5月7日)
http://blog.sina.com.cn/s/blog_502b2e970100949s.html 获取键盘输入以控制无法接受输入焦点的控件考虑一些游戏,显示图片在TPainBox,但是TPai ...
- 【msdn wpf forum翻译】如何在wpf程序(程序激活时)中捕获所有的键盘输入,而不管哪个元素获得焦点?
原文:[msdn wpf forum翻译]如何在wpf程序(程序激活时)中捕获所有的键盘输入,而不管哪个元素获得焦点? 原文链接:http://social.msdn.microsoft.com/Fo ...
- 如何在Python中快速画图——使用Jupyter notebook的魔法函数(magic function)matplotlib inline
如何在Python中快速画图--使用Jupyter notebook的魔法函数(magic function)matplotlib inline 先展示一段相关的代码: #we test the ac ...
- 如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python 注释
如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python 注释 PIP $ pip install beauti ...
- 如何在Python中加速信号处理
如何在Python中加速信号处理 This post is the eighth installment of the series of articles on the RAPIDS ecosyst ...
- Python常见面试题017: Python中是否可以获取类的所有实例
017. Python中是否可以获取类的所有实例 转载请注明出处,https://www.cnblogs.com/wuxianfeng023 出处 https://docs.python.org/zh ...
随机推荐
- Spring整合Quartz简单入门
创建一个Web项目 导入相关jar包 <?xml version="1.0" encoding="UTF-8"?> <project xmln ...
- MySQL运维8-Mycat范围分表
一.范围分片 根据指定的字段及其配置的范围与数据节点的对应情况,来决定该数据属于哪一个分片. 说明1:范围分片会提前提供一个分片的范围默认是0-500万是一个分片,500万-1000万是一个分片,10 ...
- AI量化策略会:可以直接上实盘的策略构建方法
一年一度的培训虽晚但到,这是BigQuant与大家走过的第五个培训年头,在过去的四年里看到很多学员的成长和蜕变,从一开始的懵懂无知,到现在对深度学习的信手拈来,BigQuant与各位学员们一样都收获颇 ...
- ASR项目实战-语音识别
本文深入探讨语音识别处理环节. 本阶段的重点特性为语音识别.VAD.热词.文本的时间偏移.讲话人的识别等. 语音识别 业界流派众多,比如Kaldi.端到端等,具体选择哪一种,需要综合考虑人员能力.训练 ...
- C语言汉诺塔递归算法实现
这是个目录 一.什么是递归函数 1.先看一下一个递归的例子 2.递归的基本原理 二.汉诺塔问题 1.简要概括一下汉诺塔的故事 2.回到编程,汉诺塔问题主要就是解决这个问题: 3.怎么解决汉诺塔问题 要 ...
- Spring Batch 的基本使用
简介 A lightweight, comprehensive batch framework designed to enable the development of robust batch a ...
- 解决QObject::moveToThread: Current thread (0x56059f9b0f70) is not the object's t
对 opencv 降级 pip install opencv-python==4.1.2.30
- [Python急救站]学生管理系统链接数据库
相信很多人在初学Python的时候,经常最后作业就是完成一个学生管理系统,但是我们来做一个完美的学生管理系统,并且将数据储存到数据库里. 我们先看看我们的数据库怎么设置. 首先呢,我选择用的是SQL ...
- 网络性能总不好?网络调优专家AOE帮你来“看看”
摘要:为提升网络性能.降低人工调优成本,CANN推出了自动化网络调优工具AOE,通过子图调优.算子调优与梯度调优的功能,让网络可以在AI硬件上获得最佳性能. 本文分享自华为云社区<网络性能总不好 ...
- 什么是CodeArts?
摘要:一站式.全流程.安全可信的软件开发生产线. 软件开发生产线(CodeArts)原名"软件开发平台(DevCloud)"是集华为近30年研发实践.前沿研发理念.先进研发工具为一 ...