Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8063637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:06:31+00:00 2026-06-05T11:06:31+00:00

My question is, after spawning a process, the child process is looping to get

  • 0

My question is, after spawning a process, the child process is looping to get data from its stdin. I would like to write new data to it using either Echo.Process.pipes[0].write(data) or Echo.Process.writeToChild(0,data), but both do not work. Would someone explain what’s going on? Or how do I go around this problem?

This is the error I got:

--- <exception caught here> ---
  File "/usr/local/encap/python-2.6.4/lib/python2.6/site-packages/Twisted-9.0.0-py2.6-linux-x86_64.egg/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
    why = getattr(selectable, method)()
  File "/usr/local/encap/python-2.6.4/lib/python2.6/site-packages/Twisted-9.0.0-py2.6-linux-x86_64.egg/twisted/internet/tcp.py", line 460, in doRead
    return self.protocol.dataReceived(data)
  File "pp1.py", line 30, in dataReceived
    Echo.Process.pipes[0].write(data)
exceptions.KeyError: 0

Thank You,

Q

from sys import executable
from os import environ
import os
from twisted.internet import reactor
from twisted.internet.protocol import Protocol
from twisted.internet.protocol import Factory
from twisted.internet import protocol
import sys

implementation = """\
import os
import time
import sys

print "in child", os.getpid()

while (True):
        a = raw_input("")
        if a: print a
"""


class Echo(Protocol):
    Process = None
    def dataReceived(self, data):
        if Echo.Process == None:
                pp = MyPP()
                Echo.Process = reactor.spawnProcess(pp, executable, [executable, "-c", implementation, data], env=environ, childFDs = {0:1, 1:1, 2:2})
        else:
                Echo.Process.pipes[0].write(data)
                #Echo.Process.writeToChild(0,data)
        self.transport.write(data)

class EchoFactory(Factory):
    def buildProtocol(self, addr):
        return Echo()

class MyPP(protocol.ProcessProtocol):
    def connectionMade(self):
        print "connectionMade!"
    def outReceived(self, data):
        print "out"
    def errReceived(self, data):
        print "error", data
    def processExited(self, reason):
        print "processExited"
    def processEnded(self, reason):
        print "processEnded"
        print "quitting"

reactor.listenTCP(8200, EchoFactory())
print 'in parent', os.getpid()
reactor.run()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:06:32+00:00Added an answer on June 5, 2026 at 11:06 am

    To create a new process on each incoming connection and to redirect all input data to the process’ stdin:

    #!/usr/bin/python
    from twisted.internet import reactor
    
    from twisted.internet import protocol
    
    class Echo(protocol.Protocol):
        def connectionMade(self):
            self.pp = MyPP()
            reactor.spawnProcess(self.pp, 'cat', ['cat'])
        def dataReceived(self, data):
            self.pp.transport.write(data)
        def connectionLost(self, reason):
            self.pp.transport.loseConnection()
    
    class MyPP(protocol.ProcessProtocol):
        def connectionMade(self):
            print "connectionMade!"
        def outReceived(self, data):
            print "out", data,
        def errReceived(self, data):
            print "error", data,
        def processExited(self, reason):
            print "processExited"
        def processEnded(self, reason):
            print "processEnded"
            print "quitting"
    
    factory = protocol.Factory()
    factory.protocol = Echo
    reactor.listenTCP(8200, factory)
    reactor.run()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After my last question was solved the JSON I'm receiving from the server changed
After reading many times this question and its accepted answer How to execute a
I have a very simple question: After data are inserted, they will (anytime) be
Hey guys. This is a follow-on from this question : After getting the right
Two part question: After calling the Mixpanel api I am returned something like this
This is a re-work/new question after I got my previous one quite wrong by
Design problem- one function calls all three I continuation from another question after that
This is a new question after my long rant. The problem here is, I
here's a question : After entering some data about students, i need to print
It's easier to ask the question after showing an example... MyClass a = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.