Inter-script comunication

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
nabucodonosor
Forum Newbie
Posts: 8
Joined: Tue Feb 15, 2005 3:22 am
Location: Verona, Italy
Contact:

Inter-script comunication

Post by nabucodonosor »

I'm not shure if I'm writing in the right forum...
..I have a question (maybe ingenuo):
is it possible to have more than one php script running on the server, and accessing eachother functions,variables,objects and so on?
I need to have a php program running indefinitely in background on the web server (as a "thread"). It should be able to passa and recieve data to the other php scripts that ar called by the web server in response of the web clients activity.
Thanks to anyone who can clear my doubts...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

in short, no.

in long.. possibly. You probably have to create your own a channel between the background script and the client visible ones. It shouldn't be a database.

What is this background script doing? Most hosts will not allow scripts to run indefinitely, as most will eat a processor in doing so.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

nabucodonosor
Forum Newbie
Posts: 8
Joined: Tue Feb 15, 2005 3:22 am
Location: Verona, Italy
Contact:

Post by nabucodonosor »

Thanks for replay!!!
In poor words:
the "background" script should act as a driver/monitor for a serial controller... it accepts new "write requests" to the resource and listens for fresh data coming from the resource.
Any remote client (web browser) asks the fresh data and the web server, after interrogating the "driver" script, returns the page with requested data... I thought that the better way, but the solutions are still open...
nabucodonosor
Forum Newbie
Posts: 8
Joined: Tue Feb 15, 2005 3:22 am
Location: Verona, Italy
Contact:

Post by nabucodonosor »

Weirdan wrote:you can use any standard IPC (named pipes, sockets, signals, shared memory and so on).

Here are selected links to PHP manual chapters:
http://us4.php.net/manual/en/ref.sem.php
http://us4.php.net/manual/en/ref.shmop.php
http://us2.php.net/sockets
http://us2.php.net/manual/en/function.posix-mkfifo.php
http://us2.php.net/manual/en/function.posix-kill.php
Ok,thanks!
In fact I knew that there's always the soket solution (if I'm in win32 environment I can't use the shared memory at all...wright?) but I didn't know in prectise how to "fork" a script running in background in apache and acting as a server for the secceding scripts....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

so it's a live poll of the serial connection? no pooling or anything like that? Then it doesn't need a background script. What about creating an extension to php? This way, you can write a faster processing system in C for example, and a simple interface through php.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

nabucodonosor wrote: In fact I knew that there's always the soket solution (if I'm in win32 environment I can't use the shared memory at all...wright?) but I didn't know in prectise how to "fork" a script running in background in apache and acting as a server for the secceding scripts....
Some kind of exec (http://php.net/exec) should do the trick... or you could run your background script as a service (using srvany.exe from Windows Resource Kit).
nabucodonosor
Forum Newbie
Posts: 8
Joined: Tue Feb 15, 2005 3:22 am
Location: Verona, Italy
Contact:

Post by nabucodonosor »

feyd wrote:so it's a live poll of the serial connection? no pooling or anything like that? Then it doesn't need a background script. What about creating an extension to php? This way, you can write a faster processing system in C for example, and a simple interface through php.
Do you mean to register a new wrapper for example?Mmh, that's very fashinating! But I think it's too much for me,I don't know where to begin from!
I thought also to write a C application, but I'm not practical with the IPC and not shure of there's interoperability between a php script and a C application. Because the serial device refreshes, for example, a variable every 2 seconds we could think about a global variable accessible by the script and the process... but if I would mantain the portability on win32 I can't use the shared memory, and the posix functions too...or not?
At last, there's always the possibility, in a php script, to open a soket to a certain process (a C application...). But the question is: is it possible to open a socket to another php script running on the same server?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

nabucodonosor wrote:But the question is: is it possible to open a socket to another php script running on the same server?
should be possible, but I'd avoid it as much as possible.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

feyd wrote:should be possible, but I'd avoid it as much as possible.
May I ask, why?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Weirdan wrote:May I ask, why?
It mostly comes down to personal preference. When dealing with hardware, I personally want all the speed I can get for it. To me, that means a fully compiled binary.
nabucodonosor
Forum Newbie
Posts: 8
Joined: Tue Feb 15, 2005 3:22 am
Location: Verona, Italy
Contact:

Post by nabucodonosor »

Ok, I think that the most elegant solution should be that with language extension.
But if I choose the soket comunication between 2 php threads (is it an improper terminology?) my banal problem is still operative: how to, inside a php script, launch another php script whose execution will continue separately.
Maybe another approach could be throwing a redirect from the middle of the script, so the actual script continues its own execution...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

It appears it would be better to launch background script (hereafter 'server') separately (e.g. as a winnt service) and have it continuously listen on some tcp port (just like webserver does). Then you may connect to it from any php web apps. This way you don't need to mess with execs (anyway there's no reason to have multiple servers polling the same COM port, IMO).
Post Reply