Re: Can I make a process run in background?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2007-11-06 at 20:23 +0100, Luca Paolella wrote:
> > This can be done quite easily using shared memory and/or a database to
> > share data between the scripts.
> Really? could you give me a little briefing about this method?

I'll give an example using the DB as the sharing mechanism. So you have
master script:

    MASTER (waves hello)

The MASTER can spawn off any number of child background scripts. These
can be CHILD1, CHILD2, CHILD3, ..., CHILDX. When spawning a child script
you can pass it command line parameters. One such parameter I suggest
would be a UID for the CHILD process assigned by the MASTER. Another
such parameter you should probably pass is a UID for the master process.
How you generate UIDs is up to you. You could use PIDs but if your
script runs for a long time then you may re-encounter a PID down the
line. So now themaster knows his childen via the CHILD's UID (CHUID) and
the children know the master via the MASTER's UID (MUID). Now all you
need is a database table that stores messages (a message can be
anything, serialize()'d data comes to mind as a good example). You could
have a table as follows:

    CREATE TABLE messages
    (
        id           bigint    not null   auto_increment,
        scriptUid    bigint    not null,
        message      BLOB,

        PRIMARY KEY ( id ),
        INDEX ( scriptUid )
    );

Now your master can insert messages to it's children by inserting a row
into the table with scriptUid set to the appropriate CHUID. Similarly,
children can send messages back to the master by setting scriptUid to
the MUID that spawned them. One such message might be...

    $message = array
    (
        'type' => 'childList',
        'value => array( 10001, 10002, 10003 ),
    );

    $message = serialize( $message );

This would provide the child with a list of CHUIDs for all the other
children.

This is the basics. A very simplistic example. When a child retrieves a
message it should delete all messages for it's UID less than or equal to
the highest ID it received. This way the next retrieval doesn't return
previously received messages.

Anyways, this is just one way. More complex techniques can be done by
having the master listen on a socket and having the children connect to
the socket when loaded. Then messages can be passed around via sockets.
This is probably preferable since the DB solution requires polling the
database for new messages, whereas I believe PHP sockets support wakeup
on socket data with wakeup as is standard in C sockets.

The shared memory method might be the best solution but I'm not familiar
enough with it to say for sure.

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux