Welcome Guest, Not a member yet? Register   Sign In
Using sessions for cross-script communication
#1

[eluser]richthegeek[/eluser]
Hi,

Just did a quick test with using session data to communicate between two scripts that are running, for example one is a PUSH script (using multipart datatype and usleep between pushes) and the other is a action-receiver for controlling it.

Assume in each round of PUSH, we have this code:
Code:
session_start();
print $_SESSION['input'];
... do other stuff
usleep( 5000000 );

In our AR script, any attempt to open that session will result in hanging until the PUSH script has closed due to some fancy semaphore locking. That is a good thing, but not great.

In order to allow the AR script to read/write the session in between PUSH rounds, we need to add "session_write_close()" at the end of each PUSH round.

This way, we can change the session data using the AR script and the PUSH round will pick it up next time.

Here's two files as an example of the barebones code for shoiwng it:
Code:
// "push" file
session_start();
$_SESSION['test'] = "one";
print $_SESSION['test']."<br/>";
session_write_close();

usleep(5000000);

session_start();
print $_SESSION['test'];

Code:
// "action receiver" file
session_start();
$_SESSION['test'] = rand();
session_write_close();
#2

[eluser]WanWizard[/eluser]
Which custom session library are you using? Because CI doesn't use session_start() and $_SESSION.
#3

[eluser]richthegeek[/eluser]
Oh my *facepalm*

http://php.net/manual/en/function.session-start.php

Using the built-in PHP functions for the proof-of-concept.. you know, the ones that are built in regardless of what library or framework you are using on top of it?
#4

[eluser]WanWizard[/eluser]
Nothing against that, but your making it very difficult on yourself.

How are these processes started? Sessions are cookie based, to be able to have two processes use the same session, they need the same cookie with the same session_id.

Sessions aren't meant to be used as interprocess semaphores, but as a way to simulate state in a stateless environment for a single user. If you need this, I suggest you look for a proper semaphore system, either file or memory based. PHP even has built-in semaphone functions (on real OS's only Smile ).
#5

[eluser]richthegeek[/eluser]
Yeah, it was just a bit of mucking about to see if it would work (and this was the only application I could think of for it).

Using a database would be the best solution really, although if it's in a PUSH+AR setup then only one would be writing to it, and the other would only be reading, so a semaphore isn't really needed very much. In the event that you write and read at the same time the event would only be delayed until the next round which shouldn't be too far away anyway, so no real harm there.

Semaphores with PHP tend to lock files instead of shared memory (although the OS's filesystem handling might actually have the file stored in memory anyway) which involve IO utilisation so making it somewhat inefficient.




Theme © iAndrew 2016 - Forum software by © MyBB