Multiple simultaneous XML execution

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

Multiple simultaneous XML execution

Post by firkinfedup »

Hi there,

I have a question regarding the execution of the XML DOM functions being executed by multiple users and the same time.

I have just written some XML code to replace my database driven web counters, I have done it due to limitations with my current hosting package and I also prefer to use XML anyway.

My code basically loads an XML document, searches for a particular node using the XPath objects and then increments an attribute before calling "dump_file" from the DOM object. It has just occurred to me that if multiple users were to visit my site at the same time that my counter code would either,

A) Cause an access violation as the file may be in use.
B) Cause indiscrepencies in my counters incrementation as 2 users executing the counter at the same time would cause only 1 increment (that's if an access violation doesn't occur anyway).

So that makes me think that I should be looking to develop far more complex code to get around this issue, but am unsure how to deal with this in PHP. I know that if I were to use something like ASP.NET that I could easily implement thread blocking, but how would I do this kind of thing in PHP?

More to the point, am I correct in making these assumptions or does PHP handle this silently in the background? I’ve tried to make the code as quick as possible for this reason but the final “dump_file” call may be my bottle neck as the entire file gets re-written (presumably). Anyway, I would appreciate any ideas and input on this, thanks loads in advance!

Nick.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

so you are experiencing why databases are better/easier than files already ;)


you could always use [php_man]flock[/php_man] etc and then make php sleep a while untill the file becomes available... (be aware for deadlocks though)
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

Post by firkinfedup »

Hi Tim,

"so you are experiencing why databases are better/easier than files already"

LOL, to be honest with you I'm turning into a bit of an XML freak, I use it for pretty much everything now.

"you could always use flock etc and then make php sleep a while untill the file becomes available... (be aware for deadlocks though)"

Excellent, I shall have to take a look at it, it would only mean speeping for about a second or so I should imagine, but that sounds like an excellent idea. Cheers for the tip! :-)

Nick.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

timvw wrote:so you are experiencing why databases are better/easier than files already ;)


you could always use [php_man]flock[/php_man] etc and then make php sleep a while untill the file becomes available... (be aware for deadlocks though)
LOL!
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

Post by firkinfedup »

To be honest, I'm sure you are aware of this but there is no reason that databases should be any better than using XML. You just have to be prepared to put the time into doing the extra coding. I've got an XML web counter now that uses VB.NET CGI and is capable of 100% writes, so I'm more than happy, and not a database in sight!
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

firkinfedup wrote: ...but there is no reason that databases should be any better than using XML.
This really sounds fishy. For many things, yes, XML is better, but in this case, it isn't.
firkinfedup wrote: You just have to be prepared to put the time into doing the extra coding.
This is one very good reason why it isn't. I understand that you have certain conditions to
work with, but in most cases a database would be a better solution for anything relating
to data management.

Anyways, be careful with flock! There was another discussion around here where there was talk about it's use.

Cheers
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

Fishy fishy

Post by firkinfedup »

Hi there,
This really sounds fishy. For many things, yes, XML is better, but in this
case, it isn't
Well to be honest with you I don't understand that statement. Why not? I don't feel this is a case of reinventing the wheel, just relying on a database driver to store a counter is overkill.

I haven't actually used flock and I haven't even experienced the problems I said that I *might* get, It just occured to me that this has a possibility of occuring. I've ended up writing a VB.NET CGI application that reads and writes XML, it works extremely fast and efficiently with thanks to the .NET Frameworks strong native footings with XML.

And what's more being able to write applications that *easily* download my XML data and display it in an application in a variety of ways is excellent, especially as I can edit it with notepad if I so wish.

Though I am a hypocrite, If I had the access to hundreds of databases with this particular host I would probably attempt to roll everything into that. But on the other hand, I find it far more rewarding using XML, with far less problems too I might add!

Anyway, merry christmas! :wink:

Nick.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: Fishy fishy

Post by BDKR »

firkinfedup wrote:Hi there,
This really sounds fishy. For many things, yes, XML is better, but in this
case, it isn't
Well to be honest with you I don't understand that statement. Why not? I don't feel this is a case of reinventing the wheel, just relying on a database driver to store a counter is overkill.
Good point! There have been plenty of counters in the past that didn't use a database. :-)
firkinfedup wrote: I haven't actually used flock and I haven't even experienced the problems I said that I *might* get, It just occured to me that this has a possibility of occuring. I've ended up writing a VB.NET CGI application that reads and writes XML, it works extremely fast and efficiently with thanks to the .NET Frameworks strong native footings with XML.

And what's more being able to write applications that *easily* download my XML data and display it in an application in a variety of ways is excellent, especially as I can edit it with notepad if I so wish.

Though I am a hypocrite, If I had the access to hundreds of databases with this particular host I would probably attempt to roll everything into that. But on the other hand, I find it far more rewarding using XML, with far less problems too I might add!

Anyway, merry christmas! :wink:

Nick.
I will say that XML has been the foundation for a couple of cool things that I wanted (my boss was just too dificult to work for so I left) to do. In particular, pass messegas from automated machines on a shop floor back to a central system that would also keep track of an orders progress and location through a plant. Through in some Python and .Net stuff for the ladys up front; it could've been nice.

Oh well.... :roll:

Cheers,
BDKR
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

imho:

Code: Select all

<root>
<counter>666</counter>
</root>
1) parsing the above is also a lot of overkill..
2) has synchronisation problems in php, for example the following scenario can and will happen (i can understand you haven't noticed situations like this)

client 1 requests page
client 2 requests page
counter.xml is parsed for client1 and script reads 666
counter.xml is parsed for client2 and script reads 666
counter.xml is written for client1 and counter at 667
counter.xml is written for client2 and counter at 667

it's obvious that in this case the counter should have been at 668.



I can agree that xml (because of the widely available tools to handle it) has it's advantages. I just can't see it for this type of application. If the synchronzation problems where not an issue, i would use a plain-old text file.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

databases are so commonly used now and are so accessible I see no reason why it would be overkill.
Post Reply