[Twisted-Python] some confusion with daemonising my twisted application.

hackingKK hackingkk at gmail.com
Tue Apr 26 10:04:51 EDT 2011


Hello all.
I have been using the twisted library for quite some time and I must say 
it is the best library for xmlrpc.
I have a tcp server listening on a port and I use reactor.run() to start it.
Now I want to learn how to daemonise the entire code.

I am paisting the code below.


from twisted.web import xmlrpc, server
#reactor from the twisted library starts the server with a published 
object and listens on a given port.
from twisted.internet import reactor
from time import strftime
import rpc_organisation
import rpc_vendor
import rpc_customer
import rpc_product

#rest of the modules similarly imported, each containing a class 
inherited from xmlrpc.XMLRPC

#the main class
class gnukhata(xmlrpc.XMLRPC):

#note that all the functions to be accessed by the client must have the 
xmlrpc_ prefix.
     def __init__(self):
         xmlrpc.XMLRPC.__init__(self)

#the client however will not use the prefix to call the functions.
         #self.client_id = dbconnect.getConnection()

     def xmlrpc_getOrganisationNames(self):
         #this function is used to return the list of organsations found 
in gnukhata.xml located at /etc.Returns a list of organisations already 
present in the file
         #calling the function for getting list of organisation nodes.

         orgs = dbconnect.getOrgList()
#initialising an empty list for organisation names
         orgnames = []
         for org in orgs:
             orgname=org.find("orgname")
             #checking for unique names.
             #we will use the not in clause here.
             if orgname.text not in orgnames:
                 orgnames.append(orgname.text)

         return orgnames



#similarly a few other methods for this class.

#create an instance of the class to be published as the service.
print "initialising application"
gnukhata = gnukhata()
organisation = rpc_organisation.organisation()
gnukhata.putSubHandler('organisation',organisation)
people = rpc_people.people()
gnukhata.putSubHandler('people',people)
vendor = rpc_vendor.vendor()
gnukhata.putSubHandler('vendor',vendor)

#... and so on.

#Now the code for running the server.


#publish the object and make it to listen on the given port through reactor
print "starting server"
reactor.listenTCP(7081, server.Site(gnukhata))
#start the service by running the reactor.
reactor.run()

The code is from a python script called rpc_main.py which is in a 
package gnukahta where all other classes are also kept.
  to run it right now, I get into the gnukhata directory and do a sudo 
./rpc_main.py because some actions in this file need sudo permission.

Now could some one tell me how to convert this reactor running process 
into a daemon?
right now when I run this rpc_main.py file, the terminal does not return 
the prompt.
I used ./rpc_main.py & to get the terminal back.
But on a remote server this process is naturally killed when i logout 
from the remote shell.
Besides I have to make this as a package so making a daeminised version 
to run is a good option.
I have been reading a lot on this issue but did not find any good answer.
I am not so much into the details of how twisted works except for this much.
I guess my code would reflect how little I know about this amaising library.
So I just want to know if I have to write a seperate file to run this as 
a daemon?
As a side note, I used twistd -y rpc_main.py to see what happens.
I got complaints on the imports like the one given at the top of my code.
These modules are in the same gnukhata package as the rpc_main.py itself 
and run perfectly in other conditions.
So please help, this is really confusing me.
Thanking all,
Happy hacking.
Krishnakant.




More information about the Twisted-Python mailing list