[Twisted-Python] Concurrent data access with select() safe?
Hello all, I've managed to get a working custom webserver thanks to Donovan. My next question is (perhaps more due to my ignorance of how select works) how do you control concurrent access to data? In a multi-thread system you'd normally use devices like locks on the data / resource but what about an event based one like twisted? Say my server does reads and writes on some file/files. How do I ensure data integrity? Do I need some kind of locking mechanism or does using SelectReactor make this point moot? -- Regards, Mukhsein Johari
On Wed, Aug 14, 2002 at 02:09:19AM +0800, Mukhsein Johari wrote:
I've managed to get a working custom webserver thanks to Donovan. My next question is (perhaps more due to my ignorance of how select works) how do you control concurrent access to data?
There is no concurrent access to data in a single-threaded, event-based system, because the event handlers which are run by the main loop are never interrupted. A function always runs to completion, as if it's always running in a critical section. (This is also why no blocking system calls should be performed except in the main loop itself.) -- Matt Campbell Email and MSN Messenger: mattcampbell@pobox.com Web site: http://www.pobox.com/~mattcampbell/
On Wednesday 14 August 2002 02:28, Matt Campbell wrote:
On Wed, Aug 14, 2002 at 02:09:19AM +0800, Mukhsein Johari wrote:
I've managed to get a working custom webserver thanks to Donovan. My next question is (perhaps more due to my ignorance of how select works) how do you control concurrent access to data?
There is no concurrent access to data in a single-threaded, event-based system, because the event handlers which are run by the main loop are never interrupted. A function always runs to completion, as if it's always running in a critical section. (This is also why no blocking system calls should be performed except in the main loop itself.)
Sorry for being so dense but, what do you mean by this? Does file access count as blocking (my gut says "yes")? How would you handle that? In the twisted system in general, from what I understand of it, classes are instantiated to handle client requests. If those instances access files or in-memory data objects it is conceivable that two instances might be accessing the same data / file, right? But from you you say, this is fine since there is only one thread. But what do you mean by blocking calls should only be run in the main loop? Is the above (file access) blocking? And is it, as the case stated above "within the main loop"? -- Regards, Mukhsein Johari
On Wed, Aug 14, 2002 at 03:37:09AM +0800, Mukhsein Johari wrote:
On Wednesday 14 August 2002 02:28, Matt Campbell wrote:
completion, as if it's always running in a critical section. (This is also why no blocking system calls should be performed except in the main loop itself.)
Sorry for being so dense but, what do you mean by this? Does file access count as blocking (my gut says "yes")? How would you handle that? In the twisted system in general, from what I understand of it, classes are instantiated to handle client requests. If those instances access files or in-memory data objects it is conceivable that two instances might be accessing the same data / file, right? But from you you say, this is fine since there is only one thread. But what do you mean by blocking calls should only be run in the main loop? Is the above (file access) blocking? And is it, as the case stated above "within the main loop"?
You shouldn't do anything that takes a long time; events won't be processed while the single thread runs your long operation. Matt said "(This is also why no blocking system calls should be performed except in the main loop itself.)"; well, most system calls "block", but the idea is to not run ones that can block indefinitely -- e.g. send(). Those are run by the mainloop in a non-blocking mode. -- tv@{{hq.yok.utu,havoc,gaeshido}.fi,{debian,wanderer}.org,stonesoft.com} double a,b=4,c;main(){for(;++a<2e6;c-=(b=-b)/a++);printf("%f\n",c);}
participants (3)
-
Matt Campbell
-
Mukhsein Johari
-
Tommi Virtanen