热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python中stdinput是什么_pythoncodemudule,pipe,stdin,stdou,IMPORTANT

fork是pythonlinux下os模块下的一个方法,用来创建一个子进程。今天遇到这个问题,所以找文章来稍微了解一下。以下来自http:www.mye

fork 是 python linux下 os 模块下的一个方法,用来创建一个子进程。今天遇到这个问题,所以找文章来稍微了解一下。以下来自http://www.myelin.co.nz/post/2003/3/13/#200303135。不当之处多指教。

1、有时,程序在一个进程中运行可能会遇到一些问题。如进程可能会占用过多的内存或者打开太多的文件,或者根本无法运行。

2、一般来说,需要将进程分为两个,在子进程中执行一些代码,然后向父进程总返回结果。

这个过程是通过管道来实现的。os.pipe()创建一个管道。一个管道包括两个端,一个读(父进程)一个写(子进程)。子进程将结果写入写端,然后关闭之。父进程从读端读出。

以os.fork()创建新进程,复制所有文件描述符。则父子进程都有了管道的读端和写端的拷贝。直到管道一端的所有拷贝都被关闭,那么管道才关闭,因而在创建子进程之后需要调用os.close()关闭父进程的写端。

所以,运行过程是:

---创建管道

---创建子进程。

子进程:

----需要关闭管道读端

----开始执行

----向写端写入结果

----进程死亡

父进程:

----关闭管道写端

----从读端读取数据直到子进程死亡或者关闭

----调用waitpid方法确保子进程已经被撤销(在FreeBSD中不这么做,那么子进程永远不会被死亡)

----进程输出

3、代码

#!/usr/bin/env python

import os, sys

print "I'm going to fork now - the child will write something to a pipe, and the parent will read it back"

r, w = os.pipe()           # r,w是文件描述符, 不是文件对象

pid = os.fork()

if pid:

# 父进程

os.close(w)           # 关闭一个文件描述符

r = os.fdopen(r)      # 将r转化为文件对象

print "parent: reading"

txt = r.read()

os.waitpid(pid, 0)   # 确保子进程被撤销

else:

# 子进程

os.close(r)

w = os.fdopen(w, 'w')

print "child: writing"

w.write("here's some text from the child")

w.close()

print "child: closing"

sys.exit(0)

print "parent: got it; text =", txt

note here

Re: Subprocess confusion: how file-like must stdin be?

Cameron Laird

Fri, 18 Aug 2006 05:10:51 -0700

In article ,

Nick Craig-Wood wrote:

>Dennis Lee Bieber wrote:

>> On Thu, 17 Aug 2006 17:16:25 +0000, [EMAIL PROTECTED] (Cameron Laird)

>> declaimed the following in comp.lang.python:

>>

>> > Question:

>> > import subprocess, StringIO

>> >

>> > input = StringIO.StringIO("abcdefgh\nabc\n")

>>

>> Here you override the builtin function "input()"

>> > # I don't know of a compact, evocative, and

>> > # cross-platform way to exhibit this behavior.

>> > # For now, depend on cat(1).

>> > p = subprocess.Popen(["cat"], stdout = subprocess.PIPE,

>> > stdin = response)

>>

>> Here you specify the non-existant "response"

>

>Assume the OP meant to write this

>

>>>> import subprocess, StringIO

>>>> inp = StringIO.StringIO("abcdefgh\nabc\n")

>>>> p = subprocess.Popen(["cat"], stdout = subprocess.PIPE, stdin = inp)

>Traceback (most recent call last):

> File "", line 1, in ?

> File "/usr/lib/python2.4/subprocess.py", line 534, in __init__

> (p2cread, p2cwrite,

> File "/usr/lib/python2.4/subprocess.py", line 830, in _get_handles

> p2cread = stdin.fileno()

>AttributeError: StringIO instance has no attribute 'fileno'

>>>>

.

.

.

Yes; my apologies for the confusion I introduced by "editing

for publication", and doing it badly.

Your interactive session does indeed exhibit the behavior that

puzzles me. My expectation was that StringIO and the std*

parameters to Popen() were made for each other; certainly there

are many cases where stdout and stderr can be redirected *to* a

StringIO. Is it simply the case that stdin demands a more

file-like object? While that disappoints me, I certainly can

program around it. My question, then: does stdin effectively

require something really in the filesystem, or perhaps the

stdout of a previous subprocess? Is there no built-in way to

feed it an in-memory construct?

--

http://mail.python.org/mailman/listinfo/python-list

Re: Subprocess confusion: how file-like must stdin be?

Laurent Pointal

Fri, 18 Aug 2006 06:55:59 -0700

Cameron Laird a écrit :

> In article ,

> Nick Craig-Wood wrote:

>> Dennis Lee Bieber wrote:

>>> On Thu, 17 Aug 2006 17:16:25 +0000, [EMAIL PROTECTED] (Cameron Laird)

>>> declaimed the following in comp.lang.python:

>>>

>>>> Question:

>>>> import subprocess, StringIO

>>>>

>>>> input = StringIO.StringIO("abcdefgh\nabc\n")

>>> Here you override the builtin function "input()"

>>>> # I don't know of a compact, evocative, and

>>>> # cross-platform way to exhibit this behavior.

>>>> # For now, depend on cat(1).

>>>> p = subprocess.Popen(["cat"], stdout = subprocess.PIPE,

>>>> stdin = response)

>>> Here you specify the non-existant "response"

>> Assume the OP meant to write this

>>

>>>>> import subprocess, StringIO

>>>>> inp = StringIO.StringIO("abcdefgh\nabc\n")

>>>>> p = subprocess.Popen(["cat"], stdout = subprocess.PIPE, stdin = inp)

>> Traceback (most recent call last):

>> File "", line 1, in ?

>> File "/usr/lib/python2.4/subprocess.py", line 534, in __init__

>> (p2cread, p2cwrite,

>> File "/usr/lib/python2.4/subprocess.py", line 830, in _get_handles

>> p2cread = stdin.fileno()

>> AttributeError: StringIO instance has no attribute 'fileno'

> .

> .

> .

> Yes; my apologies for the confusion I introduced by "editing

> for publication", and doing it badly.

>

> Your interactive session does indeed exhibit the behavior that

> puzzles me. My expectation was that StringIO and the std*

> parameters to Popen() were made for each other; certainly there

> are many cases where stdout and stderr can be redirected *to* a

> StringIO. Is it simply the case that stdin demands a more

> file-like object? While that disappoints me, I certainly can

> program around it. My question, then: does stdin effectively

> require something really in the filesystem, or perhaps the

> stdout of a previous subprocess? Is there no built-in way to

> feed it an in-memory construct?

As this is a pipe at OS level, there may be no other way than using

os-level tools (ie. real files with fileno), maybe creating an anonymous

pipe, writing to it (relying on pipe buffering by the OS to avoid the

creation of a writer thread), and giving this pipe (which should have a

fileno) to the subprocess.Popen stdin parameter.

Such a construction (pipe/writer thread) would be welcome as standard

subprocess tool.

A+

Laurent.

--

http://mail.python.org/mailman/listinfo/python-listReply via email to



推荐阅读
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了Python对Excel文件的读取方法,包括模块的安装和使用。通过安装xlrd、xlwt、xlutils、pyExcelerator等模块,可以实现对Excel文件的读取和处理。具体的读取方法包括打开excel文件、抓取所有sheet的名称、定位到指定的表单等。本文提供了两种定位表单的方式,并给出了相应的代码示例。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 本文介绍了在Win10上安装WinPythonHadoop的详细步骤,包括安装Python环境、安装JDK8、安装pyspark、安装Hadoop和Spark、设置环境变量、下载winutils.exe等。同时提醒注意Hadoop版本与pyspark版本的一致性,并建议重启电脑以确保安装成功。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • 树莓派Linux基础(一):查看文件系统的命令行操作
    本文介绍了在树莓派上通过SSH服务使用命令行查看文件系统的操作,包括cd命令用于变更目录、pwd命令用于显示当前目录位置、ls命令用于显示文件和目录列表。详细讲解了这些命令的使用方法和注意事项。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
author-avatar
董可芳妍_731
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有