You are not logged in.

#1 2009-05-18 10:55:53

passbe
Member
Registered: 2009-03-14
Posts: 74
Website

Python Events - Server / Client communication

I am (attempting) to write a program that tracks certain events in python. I currently have a server daemon (haven't got it forking yet) that keeps track of these events. It works as follows:

- Server starts.
- Parses event XML file and adds previous events into memory.
- Sorts these events
- Sits and waits for new events.

The very same server program allows you to send new events. For example a very simple use, wget url && eventsystem -a "Wget finished" -p "wget". Once the server receives an event, it parses it adds it to the array and writes it to the XML file. Other functionality includes eventsystem -l lists the current events. The system keeps a user defined amount of events. All this is not a problem, i have coded a prototype and it is working very well.

I was going to code a gui with the server however I would prefer a server / client system like MPD, allowing users to program their own clients. So my question is how would I begin to code such a relationship, would the client ask the server for its list of events every so often, or can / would it be better practice to allow the server to send a message to all connected clients alerting them of the new event (this is the preferred method, as the alert delay is an important factor). I'd like to start console based, so just getting a python client connected to the server and listing the events.

I know this question is a tad vague, but could anyone offer some insight, documentation or ideas to help me ? Additionally is there a universal class for python daemonizing a program? smile

Offline

#2 2009-05-18 11:03:30

mike_93
Member
Registered: 2009-01-31
Posts: 60

Re: Python Events - Server / Client communication

passbe wrote:

(haven't got it forking yet)

I assume you mean working?


Double booting Arch Linux and Linux Mint
Reader of XKCD

Offline

#3 2009-05-18 11:08:49

passbe
Member
Registered: 2009-03-14
Posts: 74
Website

Re: Python Events - Server / Client communication

no i mean forking, as in running it like a daemon. on a side note im using the SimpleXMLRPCServer class for sending new alerts to the server program.

Offline

#4 2009-05-18 11:13:49

X/ax
Member
From: Oost vlaanderen, Belgium
Registered: 2008-01-13
Posts: 275
Website

Re: Python Events - Server / Client communication

For the forking (if you didn't already have it), this is a daemonize function I encountered once online. Don't really know the source anymore, anyhow, credits to that person!

1362 cpf@Vvardenfell ~/school/Project_pysm/pysm/homeless % cat daemonize.py 
import os, sys

def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
    '''This forks the current process into a daemon.
    The stdin, stdout, and stderr arguments are file names that
    will be opened and be used to replace the standard file descriptors
    in sys.stdin, sys.stdout, and sys.stderr.
    These arguments are optional and default to /dev/null.
    Note that stderr is opened unbuffered, so
    if it shares a file with stdout then interleaved output
    may not appear in the order that you expect.
    '''
    # Get initial dir.
    first_dir = os.getcwd()

    # Do first fork.
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0) # Exit first parent.
    except OSError, e:
        sys.stderr.write ("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)

    # Decouple from parent environment.
    os.chdir("/")
    os.umask(0)
    os.setsid()

    # Do second fork.
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0) # Exit second parent.
    except OSError, e:
        sys.stderr.write ("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)

    # Now I am a daemon!

For server programming (and if I understand what you mean with events properly) you could use twisted. Programming a Factory + Protocol will get you a long way.


My coding blog (or an attempt at it)
Archer start page (or an attempt at it)

Offline

#5 2009-05-18 11:24:48

passbe
Member
Registered: 2009-03-14
Posts: 74
Website

Re: Python Events - Server / Client communication

Thank you for the daemon code. Ill have to look into twisted, it looks very good, im a web developer by trade, so desktop programming is not my strong point.

Offline

#6 2009-05-18 20:25:14

Yannick_LM
Member
Registered: 2008-12-22
Posts: 142

Re: Python Events - Server / Client communication

Additionally is there a universal class for python daemonizing a program?

The threading module http://docs.python.org/library/threading.html offers a way to create a thread with an attribute isdaemon.

Could be useful.  The program will when no alive non-daemon threads are left.

would the client ask the server for its list of events every so often, or can / would it be better practice to allow the server to send a message to all connected clients alerting them of the new event (this is the preferred method, as the alert delay is an important factor)

For this I don't know...

Question: did you write this from scratch or did you use something like xmlrpc ?
(just tried of few things with this lib, only to discover you could not send a None value, and that there's a wonderful XML-RPC introspection)

Offline

#7 2009-05-18 21:37:22

passbe
Member
Registered: 2009-03-14
Posts: 74
Website

Re: Python Events - Server / Client communication

Thank you Yannick_LM, ill have to read up on the threading link. I have written this application from scratch yes, but im about to re-write it with twisted to see how I go with the server / client arch.

Offline

Board footer

Powered by FluxBB