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 621291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:51:30+00:00 2026-05-13T18:51:30+00:00

I have been creating an async server socket that sends and recives xml using

  • 0

I have been creating an async server socket that sends and recives xml using twisted.

The application works great! but because my main objective was to embed it in an init.d script and make it run in the background i decided to transform it in a “twisted application” in order to run it using twistd

# from twisted.internet import reactor
from twisted.internet.protocol import ServerFactory
from twisted.protocols.basic import LineOnlyReceiver
from twisted.application import internet, service

from xml.etree import ElementTree as ET

from aMuleClass import amulecmd

class DialogueProtocol(LineOnlyReceiver):
    def connectionMade(self):
        print "Connected: %s" % self.transport.getPeer().host
def lineReceived(self, line):
    parsed= ET.XML(line)
    if parsed.attrib['type'] == 'request':
        if parsed.attrib['prompt'] == 'results':
            self.transport.write(self.factory.mule.results())
        elif parsed.attrib['prompt'] == 'downloads':
            self.transport.write(self.factory.mule.downloads())
        else:
            print "Invalid request: %s\n" % line
    else:
        query= parsed.attrib['value']
        if parsed.attrib['type'] == 'search':
            print "must search for %s" % query
            self.factory.mule.search(query)
        elif parsed.attrib['type'] == 'cancel':
            print "must cancel %s" % query
            self.factory.mule.command("cancel %s" % query)
        elif parsed.attrib['type'] == 'download':
            print "must download %s" % query
            self.factory.mule.command("download %s" % query)

class DialogueProtocolFactory(ServerFactory):
def __init__(self):
    self.protocol= DialogueProtocol
    self.mule= amulecmd()

def main():
factory= DialogueProtocolFactory()
port = 14000
#
daemon= internet.TCPServer(port, factory)
application= service.Application("aMuleSocket")
#
daemon.setServiceParent(application)

if __name__ == '__main__':
main()

Running this with “twistd -noy file” (debug) works PERFECTLY.
The problem is when i want to background my script! (“twistd -y file”) the socket dosent respond and the log gets filled with errors from pexpect, which is imported in my amulecmd class…
pexpect communicates with a terminal-prompt application and returns the answers to the socket..

logfile:

2010/02/17 19:54 +0200 [-] Log opened.
2010/02/17 19:54 +0200 [-] twistd 2.5.0 (/usr/bin/python 2.5.2) starting up
2010/02/17 19:54 +0200 [-] reactor class: <class          'twisted.internet.selectreactor.SelectReactor'>
2010/02/17 19:54 +0200 [-] Loading aMuleSocket.tac...
2010/02/17 19:54 +0200 [-] Starting parent
2010/02/17 19:54 +0200 [-] Loaded.
2010/02/17 19:54 +0200 [-] __builtin__.DialogueProtocolFactory starting on 2000
2010/02/17 19:54 +0200 [-] Starting factory <__builtin__.DialogueProtocolFactory instance at 0x82dbd8c>
2010/02/17 19:54 +0200 [__builtin__.DialogueProtocolFactory] Connected: 192.168.0.2
2010/02/17 19:54 +0200 [DialogueProtocol,0,192.168.0.2] Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/twisted/python/log.py", line 48, in      callWithLogger
 return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib/python2.5/site-packages/twisted/python/log.py", line 33, in callWithContext
 return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib/python2.5/site-packages/twisted/python/context.py", line 59, in callWithContext
 return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib/python2.5/site-packages/twisted/python/context.py", line 37, in callWithContext
 return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib/python2.5/site-packages/twisted/internet/selectreactor.py", line 139, in _doReadOrWrite
 why = getattr(selectable, method)()
File "/usr/lib/python2.5/site-packages/twisted/internet/tcp.py", line 362, in doRead
 return self.protocol.dataReceived(data)
File "/usr/lib/python2.5/site-packages/twisted/protocols/basic.py", line 149, in dataReceived
 self.lineReceived(line)
File "aMuleSocket.tac", line 19, in lineReceived
 self.transport.write(self.factory.mule.downloads())
File "/home/hecyra/amule_scripts/amule-remote-read-only/server/aMuleClass.py", line 60, in downloads
 list= self.command('show DL').splitlines()
File "/home/hecyra/amule_scripts/amule-remote-read-only/server/aMuleClass.py", line 42, in command
 self.prompt()
File "/home/hecyra/amule_scripts/amule-remote-read-only/server/aMuleClass.py", line 27, in prompt
 self.process.expect('aMulecmd')
File "/usr/lib/python2.5/site-packages/pexpect.py", line 1064, in expect
 return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
File "/usr/lib/python2.5/site-packages/pexpect.py", line 1116, in expect_list
 c = self.read_nonblocking (self.maxread, timeout)
File "/usr/lib/python2.5/site-packages/pexpect.py", line 656, in read_nonblocking
 if not self.isalive():
File "/usr/lib/python2.5/site-packages/pexpect.py", line 914, in isalive
raise ExceptionPexpect ('isalive() encountered condition where "terminated" is 0, but   there was no child process. Did someone else call waitpid() on our process?')
pexpect.ExceptionPexpect: isalive() encountered condition where "terminated" is 0,            but there was no child process. Did someone else call waitpid() on our process?

What could it be?? all i need is to background this script 🙁 looked easy

  • 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-05-13T18:51:30+00:00Added an answer on May 13, 2026 at 6:51 pm

    You’re spawning a child process before daemonizing. After daemonizing that child is now a child of init, and not a child of your daemon.

    You need to subclass from twisted.application.service import Service and spawn the child process in startService, which will be called after daemonizing.

    Á La: Twisted network client with multiprocessing workers?

    Edit: implementation

    I can’t test this entirely as I don’t have your amulecmd, but try something more like this:

    #!/usr/bin/env python
    # vim:ai:et:ts=2:sw=2:bg=dark
    from twisted.internet import protocol
    from twisted.protocols.basic import LineOnlyReceiver
    from twisted.application import service
    
    
    from xml.etree import ElementTree as ET
    
    from aMuleClass import amulecmd
    
    class DialogueProtocol(LineOnlyReceiver):
      def connectionMade(self):
        print "Connected: %s" % self.transport.getPeer().host
      def lineReceived(self, line):
        parsed= ET.XML(line)
        if parsed.attrib['type'] == 'request':
          if parsed.attrib['prompt'] == 'results':
            self.transport.write(self.factory.mule.results())
          elif parsed.attrib['prompt'] == 'downloads':
            self.transport.write(self.factory.mule.downloads())
          else:
            print "Invalid request: %s\n" % line
        else:
          query= parsed.attrib['value']
          if parsed.attrib['type'] == 'search':
            print "must search for %s" % query
            self.factory.mule.search(query)
          elif parsed.attrib['type'] == 'cancel':
            print "must cancel %s" % query
            self.factory.mule.command("cancel %s" % query)
          elif parsed.attrib['type'] == 'download':
            print "must download %s" % query
            self.factory.mule.command("download %s" % query)
    
    class MyService(service.Service):
      def __init__(self,port=14000):
        self.port = port
      def startService(self):
        self.factory = protocol.Factory()
        self.factory.protocol = DialogueProtocol
        from twisted.internet import reactor
        reactor.callWhenRunning(self.startListening)
      def startListening(self):
        self.factory.mule = amulecmd()
        from twisted.internet import reactor
        self.listener = reactor.listenTCP(self.port,self.factory)
        print "Started listening"
      def stopService(self):
        self.listener.stopListening()
    
    if __name__ == '__main__':
      pass
    else:
      application = service.Application("aMuleSocket")
      services = service.IServiceCollection(application)
      MyService().setServiceParent(services)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 304k
  • Answers 304k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try inline content-disposition, however I have to check this: Response.ContentType… May 13, 2026 at 8:53 pm
  • Editorial Team
    Editorial Team added an answer Is your WebConfiguration the same on both dev and live?… May 13, 2026 at 8:53 pm
  • Editorial Team
    Editorial Team added an answer You're probably thinking about the ExternalProject module added in CMake… May 13, 2026 at 8:53 pm

Related Questions

Okay, I must be missing something utterly simple here, because I've been googling for
I've been spending some time refactoring my C# code, and I'm struck by how
I currently use Amazon S3 as a backup location for my local machines -
I have been creating Unit tests like crazy and find that I'm often having
I have been creating a website with Ruby on Rails, and will be hosting

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.