Guaranty one and only one actor is running

60 views
Skip to first unread message

jabber...@gmail.com

unread,
Oct 14, 2013, 12:10:22 PM10/14/13
to python...@googlegroups.com
Say I code a memcache equivalent as an actor : I want to start it only if it's not already started.

I also want this actor to have a socket open to the rest of the world so they can talk to me with the memcache protocole, even if they are not part of my program.

How do I do that with pulsar ?

Or can I guaranty that at least one but only one arbitrer with a special UUID is running ?

lsbardel

unread,
Oct 14, 2013, 12:31:29 PM10/14/13
to python...@googlegroups.com
Hi


On Monday, October 14, 2013 5:10:22 PM UTC+1, jabber...@gmail.com wrote:
Say I code a memcache equivalent as an actor : I want to start it only if it's not already started.

I also want this actor to have a socket open to the rest of the world so they can talk to me with the memcache protocole, even if they are not part of my program.

How do I do that with pulsar ?

There are several way of doing that but the easiest is to use the socket application


In this case all you need to implement is the protocol consumer:

class MemCache(pulsar.ProtocolConsumer):

    def data_received(self, data):
        # do the parsing here
        ...
        # once you have the full message write the response back to the client
        self.transport.write(response)

if __name__ == '__main__':
    SocketServer(MemCache).start()
 
Check the echo server/client tutorial as well


Or can I guaranty that at least one but only one arbitrer with a special UUID is running ?

Not sure what you mean here. The arbiter runs the asynchronous engine.

jabber...@gmail.com

unread,
Oct 14, 2013, 3:17:54 PM10/14/13
to python...@googlegroups.com
Hi,

Thanks, but it answers only part of the question. The most important part is "I want to start it only if it's not already started. ".

If I run serveral Python VM with this code, it will starts the server several times. If the server dies, nothing will restart it. I could use supervisor for that, but I'm looking for a programmative way of doing it.

lsbardel

unread,
Oct 14, 2013, 4:25:43 PM10/14/13
to python...@googlegroups.com

Thanks, but it answers only part of the question. The most important part is "I want to start it only if it's not already started. ".

If I run serveral Python VM with this code, it will starts the server several times. If the server dies, nothing will restart it. I could use supervisor for that, but I'm looking for a programmative way of doing it.

You can use the *--pid pidfile* option in the command line to make sure only one is running (use the same pidfile location for all your VM in the same machine).
The pidfile can also be specified on the config file of your application.


Does this answer your question?

jabber...@gmail.com

unread,
Oct 14, 2013, 5:06:26 PM10/14/13
to python...@googlegroups.com
Well, this doesn't ensure one is running : if it crashes, the PID file will be stale and you have to check if your program is running manually with a process that can be different from program to program or from os to os.

lsbardel

unread,
Oct 15, 2013, 4:22:14 AM10/15/13
to python...@googlegroups.com

Well, this doesn't ensure one is running : if it crashes, the PID file will be stale and you have to check if your program is running manually with a process that can be different from program to program or from os to os.

The pid file contains the process id of the running server.
In case the server crashes without removing the pid file, you can check if that process is actually running by using the *psutil* package for example, in a programmatic fashion.

Alternatively, you can create an application which starts listening to an address only if that address is not responding to a ping.

Something along these lines:

    class Supervisor(pulsar.Application):
          def monitor_task(self, monitor):
               start = False
               try:
                    yield do_the_ping_here(...)
               except socket.error:
                    # There is no server
                    start = True
               if start:
                    yield pulsar.send('arbiter', 'run', start_server)

    def start_server(actor):
           SocketServer(...).start()

There are several other ways of doing this, ultimately the best solution will depends on the actual use case.
Reply all
Reply to author
Forward
0 new messages