Re: Listen/Notify?

Manlio Perillo <[email protected]>
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <[email protected]>
Andrea Gavana ha scritto:
> Hi All,
> 
>     sorry for my newbieness, I really know too little about pysqlite.
> I was wondering if pysqlite supports commands like LISTEN/NOTIFY,
> which I could use to update my GUI when the database "sees" a change
> made by another user. My application is a multiuser graphical
> interface, and I would like to update the GUI based on database
> modifications. The actions I would do are:
> 
> - put an INSERT/UPDATE/DELETE trigger on the table(s) I
>   want to be informed about on change
> - in the trigger send a NOTIFY
> - in the frontend start a new thread which LISTENs to the
>   NOTIFYs sent by the backend
> - when a NOTIFY arrives post an appropriate event into your
>   GUI thread (I usually use wx.CallAfter)
> 
> It is a very good suggestion I got from a wxPython user, and I just
> wanted to know if it is possible to do it with pysqlite (he is using
> PostgreSQL).
> 

SQLite does not support this.
However you can just define a function for sending a packet via UDP to 
registered listeners.

As an example:

_listeners = set()
_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def register_listener(host, port):
     _listeners.add((host, int(port)))

def send_notify(name):
     packet = struct.pack('!H', len(name)) + name
     for host, port in _listeners:
         _socket.sendto(packet, (host, port))


conn = sqlite.connect(':memory:')
conn.create_function('register_listener', 2, register_listener)
conn.create_function('send_notify', 1, send_notify)



Regards   Manlio Perillo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.