473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PHP blocks session_start while other PHP instance is running

A while ago, I implemented an AJAX-based progress bar on a web page
that executed a lengthy server operation. The web page was a Java
servlet on a Tomcat server. The progress bar worked by repeatedly
querying a secondary "Status" servlet, which returned JSON information
about the progress of the ongoing server operation. The main servlet
communicated with the "Status" servlet through session variables, so
that the status servlet would know the progress of the main servlet's
operation.

Now I am trying to implement the same thing in a different application
in PHP (which I'm more familiar with than Java servlets). Technically,
the whole thing is functional: The main script launches, opens a
session with start_session, then updates the session variables with
the progress of the operation, while the jQuery-based page requests
progress updates from the second script.

However, a fatal flaw is rendering the whole set-up useless: The
server refuses to respond to the AJAX request until the server
operation is complete. While the main script is running, the second
script cannot loaded.

By experimenting a bit, I finally traced this to the "session_start"
command. It seems that while a PHP script is being executed (using
CGI) and has a session open, trying to re-open the same session in
another script will block the second request until the first script
has finished. So the session is useless for acting as a "semaphore" to
exchange information between the two scripts, which is exactly what I
need for my progress bar.

Is there any other possibility of exchanging such information, besides
a database? I'll use the database if I have to, but it seems like a
pretty big performance drag to have to read and write the progress to
and from the database almost every second.

--
Christoph
Apr 2 '08 #1
4 2531
Hi,

On Wed, 2 Apr 2008 08:10:22 -0700 (PDT) Christoph
<ch****************@gmail.comwrote:
However, a fatal flaw is rendering the whole set-up useless: The
server refuses to respond to the AJAX request until the server
operation is complete. While the main script is running, the second
script cannot loaded.
I'd say the fatal flaw is to have a web server called script do the
lenghty task, but that's just my opinion (not unsubstantiated, though).

After all the built-in file based session mechanism chose to not deal
with concurrent access to session data and thus just locks the session
file until end of request or -- alternatively -- session_write_close().

Since you intend to use sessions as a vehicle for concurrent data
store/retrieval, that's probably not an option which would help you.
Using fine-locked files or databases or shared memory is probably
better for your task. Have a look at shmop_*(), but it might be too
restricted for your task. Otherwise, rely on files, database, ...

-hwh
Apr 2 '08 #2
On Apr 2, 5:24*pm, Hans-Werner Hilse <hi...@web.dewrote:
I'd say the fatal flaw is to have a web server called script do the
lenghty task, but that's just my opinion (not unsubstantiated, though).

After all the built-in file based session mechanism chose to not deal
with concurrent access to session data and thus just locks the session
file until end of request or -- alternatively -- session_write_close().

Since you intend to use sessions as a vehicle for concurrent data
store/retrieval, that's probably not an option which would help you.
Using fine-locked files or databases or shared memory is probably
better for your task. Have a look at shmop_*(), but it might be too
restricted for your task. Otherwise, rely on files, database, ...

-hwh
Mh... I used to sing PHP's praises over Java servlets for some time,
but now that I've worked with an event-based server I'm finding it
difficult to go back. ;)

Anyway, If the task took longer, I'd distribute it over multiple cron
calls - but I'm talking about something between 10 seconds to at most
5 minutes, so it'd be inconvenient to have to schedule it and come
back later. I guess I'll just use the database... though I'll explore
the shared memory thing for a bit.

Thanks!

--
Chris
Apr 2 '08 #3
Christoph wrote:
Is there any other possibility of exchanging such information, besides
a database? I'll use the database if I have to, but it seems like a
pretty big performance drag to have to read and write the progress to
and from the database almost every second.
I think most fast way will be shared memory
http://www.php.net/manual/en/ref.shmop.php

If you have to use mysql database use CREATE TABLE ... ENGINE =
MEMORY; to decrease overhead.
Apr 2 '08 #4
On 2 Apr, 16:10, Christoph <christoph.bursc...@gmail.comwrote:
>
By experimenting a bit, I finally traced this to the "session_start"
command. It seems that while a PHP script is being executed (using
CGI) and has a session open, trying to re-open the same session in
another script will block the second request until the first script
has finished. So the session is useless for acting as a "semaphore" to
exchange information between the two scripts, which is exactly what I
need for my progress bar.

Is there any other possibility of exchanging such information, besides
a database? I'll use the database if I have to, but it seems like a
pretty big performance drag to have to read and write the progress to
and from the database almost every second.

--
Christoph
Sounds a bit strange.

Are you running on MS Windows? If so then that might explain it -
MSWin is very heavy-handed in its file locking semantics - so if the
session file is opened at session_start() and not closed till the
sesion is flushed then that would explain it. (it might be the same on
Unix too). It seems remarkable that PHP should work that way though.

If it is due to file locking then the solution is quite simple - just
write your own session handler which does not retain an open file
handle, or use a database bound session handler.

C.
Apr 4 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Trogdor | last post by:
I set up a server on an AMD 650 machine running gentoo linux. I installed Apachie 2, MySQL 4.1 and PHP 4.3.11 I use another computer on my local net (192.168.0.x) to access the server as a...
17
by: Nodir Gulyamov | last post by:
Hello All! I met some strange situation. In MainThread my program wating changes of some variable. This variable should be changed in another thread, but loop, which wait changing variable blocks...
6
by: Jeff Smythe | last post by:
Why does Session_Start in Global.asax fire for every page opened during a session of an ASP.NET application? Am I wrong to expect that it would fire only when the first page (i.e., any page in...
5
by: Niklas Uhlin | last post by:
Someone please explain why Session_Start fires multiple times / retains SessionID values between sessions, when you open an ASP.NET page from MS Word. For details of the problem, see below: 1....
2
by: Kin | last post by:
I am trying to read the output of an external application using redirected stdout and StreamReader::ReadLine(). The problem is that ReadLine() blocks and I am either reading nothing or just part...
5
by: paladin.rithe | last post by:
I'm running into an issue with session_start(). I see that you can't run it twice, otherwise it causes an issue. That's fine, and makes sense. I also saw some ideas on how to get around this if...
14
by: Sergei Shelukhin | last post by:
Hi. I have a session started in php and two browser windows (IE)/tabs (FF) open. In one window, I execute a very slow report, immediately after that, I execute a fast simple page in another. I...
1
by: rockdale | last post by:
Hi: I have a web application which runs fine on our production server. But now when I install abother production server, it gets "System.NullReferenceException: Object reference not set to an...
5
by: greg | last post by:
Written in Asp.Net 2.0 The session_start fires on Development server running withing Visual Studio 2005 and also if access web site via localhost on development machine. But if copy to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.