We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › two php requests at the same time.
Page Index Toggle Pages: 1
two php requests at the same time. (Read 1559 times)
two php requests at the same time.
Dec 3rd, 2009, 5:10pm
 
i'm working in a sketch that loads data from a php page via loadstring().
but, if several users use my applet and execute the loadstring("http://somedomain.com/somefile.php") at the same time , what happens? will php execute them at the same time? one at a time?

lets think about a php script that reads and writes to a text file, lets say... it takes one second from start to finish the script, what happens? will a writing process start before the previous one finish?

i'm so sorry if this is not the right place to post about php, and... only about php, but i need it to  processing work i'm doing.
Re: two php requests at the same time.
Reply #1 - Dec 4th, 2009, 1:49am
 
You posted in the right forum.
You ask a classical question about concurrency.

First, concurrent access to your PHP script: it will work fine, servers are designed to answer thousands of request at once (well, in a short time span).
I doubt the script will take 1s to finish, it is unusually slow for such program! (Unless it writes MB of data on disk)
But even if you count in milliseconds, there is still a risk of clash.

So, second, concurrent access to a shared resource. To answer this, classical answer is to use a database: it will do the hard work of serializing the requests of update, managing conflicts, etc.
If you really want to use a text file, you have to lock it before access, but then other scripts might be blocked and you will loose data, etc.

Note that it is a problem only if you think you will have LOT of people using your sketch simultaneously: in most sites, you are happy if you have a visitor per hour (or one per day for my blog! Tongue), or one per minute for some commercial sites. Unless you are really very popular...
Re: two php requests at the same time.
Reply #2 - Dec 4th, 2009, 7:55am
 
ok, ok... hum.... i understand what you mean... my fears can became true.
tell me what you think about this PhiLho,

lets pretend i have 100 users logged at all the time,

and i write something like this:

in the data base:

bool jobdonne; // value stored in MySQL to check if script is running.

Table taskqueue. // table where i store updates to the txt file.

then, in the script:

1* user sends data to web site.

2* data is stored in the taskqueue table database with some common script and keeps somekind of id of the record sent to the data base.

3* after data is stored in database checks jobdonne value.

if jobdonne is true, does nothing, it means updating script is running. job ends to that user.

but if jobdonne is false, and that means the updating script is not in use, them
changes jobdonne to true, to prevents other users from activating the updatefile script,
gets all the data with his inserted record and earlier records from
taskqueue table, update the txt file,
them changes jobdonne to false back again.

---- END ----


has we say in portugal, does this ideia have legs to walk?
to prevent failures when several users update the same text file?

the text file has a x number of lines tha is always the same, that doesn't change, only values in the lines.
Re: two php requests at the same time.
Reply #3 - Dec 4th, 2009, 9:19am
 
If you use a database, why don't you store your lines in it as well?
If the file must be downloaded, it can be served by a PHP script extracting the data and sending it.
A bit like your idea, if I understood it correctly, but pushed even further, as no text file is actually written on the server!
Re: two php requests at the same time.
Reply #4 - Dec 4th, 2009, 11:26am
 
i'll try to explain better....

i'll have, something what i can call "smal files", but i'll have lots of files.
those files will be geometry vertices processing will use.  each file will cover a section of the main geometry that users will work on.

like in a building site(main geometry), several workers(users) in a level(file), where each level is a file, and those workers will acess only that level(file), but with time, the building(main geometry) will grow up and that means more file, and i want every user when loading or saving his data, doesn't need do acess ALL the data in the database, but a small section of the main geometry stored in a small file.

a user will work in a file only to prevent the server to process everything all the time.

it's possible to have 2 users updating the same data at the same time, thats no problem, because i thought about that already, i just need to prevent the script updating each file not to corrupt the file, because that would be total FAIL, since is geometry data.

so, my ideia is to make a writing_file_script that will not  be executed
twice at the same time. THAT JUST CAN'T HAPPEN. and as you told me is is possible to happen, right?

if script is executed, it blocks it's self from execution and processes data from data_queue or something, after doing so, it unblocks it's self to be executed again. if script is blocked , data send by user goes to the data_queue only, if not blocked data goes to data queue the same way, but script is activated to process all data in data_queue.

i hoppe it works fine.....
Re: two php requests at the same time.
Reply #5 - Dec 4th, 2009, 1:18pm
 
I am not sure I fully understood the whole process, but again a database is well suited for this task.

"every user when loading or saving his data, doesn't need do acess ALL the data in the database"
I think you haven't fully understood how a database works.
First, the database is a process of its own, having optimized ways to access its data, one record at a time or on a larger level, depending on what it is asked.
The HTTP server is another process, independent of the data base. On requests, it will run a PHP interpreter, which will issue requests to the database, again fetching only the data needed for a given user, for its area.

Making a proper database structure for your needs might be tricky, but probably less that doing convoluted ways (prone to fail...) to avoid concurrent writings and file corruption.
Page Index Toggle Pages: 1