2

I am a self taught Python programmer and I have an idea for a project that I'd like to use to better my understanding of Socket programming and networking in general. I was hoping that someone would be able to tell me if I am on the right path, or point me in another direction.

The general idea is to be able to update a database via a website UI, then a Python Server would then be consistently checking that database for changes. If it notices changes it would then hand out a set of instructions to the first available connected client.

The objective is to
A - Create a server that reads from a database.
B - Create clients that connect to said server from a remote machine.
C - The server would then consistently read the database and and look for changes, for instance a column that is a Boolean that would signify Run/Don't run.
D - If say the run Boolean is true, the server than would hand the instructions off to the first available client.
E - The client itself would then handle updating the database of certain runtime occurrences

Questions/Concerns
A - My first concern is the resources it would take to be constantly reading the database and searching for changes. Is there a better way of doing this? Or Could I write a loop to do this and not worry much about it?
B - I have been reading the documentation/tutorials on Twisted and at this moment this looks like a viable option to be able to handle the connections of multiple clients (20-30 for arguments sake). From what I've read Threading looks to be more of a hassle than it's worth.

Am I on the right track? Any suggestions? Or reading material worth looking at?
Thank you in advance

over
  • 33
  • 1
  • 1
  • 6
  • Whatever way you want to do it, since you are just starting on it I would recommend not to worry about performance. Just implement the best solution you can figure out. – Pratik Mandrekar Oct 05 '12 at 18:17
  • Twisted will get you twisted up, not unlike the [cover of its book](http://ecx.images-amazon.com/images/I/51Qqr380niL._SL500_AA300_.jpg) – Burhan Khalid Oct 05 '12 at 18:24

1 Answers1

1

The Django web framework implements something called Signals which at the ORM level lets you detect changes to specific database objects and attach handlers to those changes. Since it is an open source project, you might want to check the source code to understand how Django does it while supporting multiple database backends (Mysql, postgres, oracle, sqlite).

If you directly want to listen to database changes then most databases have some kind of a system that logs every transactional change. For example MySql has the binary log you can keep reading from to detect changes to the database.

Also while Twisted is great I would recommend you use Gevent/Greenlet + this. If you want to integrate with Django, how to combine django plus gevent the basics? would help

Community
  • 1
  • 1
Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65
  • 1
    [sqlalchemy provides Events](http://docs.sqlalchemy.org/en/rel_0_7/core/event.html) if it is used to work with a database. – jfs Oct 05 '12 at 23:18