2

I have written a simple twisted server -

from twisted.internet import reactor
from twisted.internet import protocol
from twisted.web import server, resource
from twisted.internet import reactor

class Index(resource.Resource):
    isLeaf = True
    def render_GET(self, request):
        args = request.args
        print 'Args: %s' %(repr(args))

print 'Serving on PORT: 8090'
site = server.Site(Index())
reactor.listenTCP(8090, site)
reactor.run()

This runs fine on 127.0.0.1:8090. Note this this runs in terminal (foreground), when I make the process run in background using nohup & ctrl+Z. the server does not respond to requests. What should I do to daemonize this twisted server

octosquidopus
  • 3,517
  • 8
  • 35
  • 53
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264

2 Answers2

9

I'd recommend looking into twistd. That way you don't have to worry about handling any of the start up, pid file management, etc. The documentation on their site is quite good: http://twistedmatrix.com/documents/current/core/howto/basics.html. Also check http://twistedmatrix.com/documents/current/core/howto/tap.html for how to implement the application file.

Mark Loeser
  • 17,657
  • 2
  • 26
  • 34
3

As nmichael and Rakis already mentioned, after "ctrl+z" type "bg" to resume suspended process as a background job.

To run it directly as background job, type

python myserver.py &

To run it directly as background job that won't stop when you logout, type

nohup python myserver.py &

Also note that nohup, is not true deamonization. See the differences here: What's the difference between nohup and a daemon?

If you really want to deamonize your Twisted server, the best option is to use twistd as Mark Loeser answered.

Community
  • 1
  • 1
ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235
  • thanks. have already done this & it works. since nmicheal's answers was not an actual ansewer, could not upvote or mark... – Srikar Appalaraju Jan 09 '11 at 14:06
  • This is not really daemonization. For example, when you close your terminal window, stdin, stdout, and stderr will simply disappear. – Glyph Dec 09 '14 at 14:19
  • @Glyph of course. That's why I have the last sentence *"But if you really want to deamonize it, use twistd as Mark Loeser answered."* I only answered on how to run in the background or with `nohup`, not deamonizing it. – ypercubeᵀᴹ Dec 09 '14 at 14:22