1

Hi i am not a big expert of Server side. I have written a long web service function which does a number of queries and processes and usually sometimes it send push notifications at the end of function to 1-20 iPhone users.

Now while writing sending massive scale push notifications to iPhone users, u have to put a delay of 1 second in between each push. This will make the simple api query takes so long like 30 seconds.

I want to avoid this , is there any way when i call web api, it runs all code and start a new thread or a page inside server and send push notifications without stalling original api and send data back to the mobile app meanwhile running a function separately on server to send push notifications.

We don't want to use any cron jobs also the web api is calculating list of users so push function can only be called inside.

$this->sendPushNotification($friendsList, $userId);
                    $response = array(
                            'error'=>false,
                            'invalidKey'=>false,
                            'message'=>"Your query has completed"
                            );
                    $this->jsonOutput($response);

Now i want to send jsonOutput at soon as the query is complete without waiting for sendPushNotification to compelte. Simply put i want to run the function asynchronously in php .

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193

1 Answers1

1

I believe you want to spawn or fork a PHP process for each item in the queue.

Have a look at these two questions. Read about pthreads. There's also a great post here on multitasking and async processing:

<?php

function work() {
    /* Do some work */

    return 'result';
}    

$dispatcher = new Amp\Thread\Dispatcher;

// call 2 functions to be executed asynchronously
$promise1 = $dispatcher->call('work');
$promise2 = $dispatcher->call('work');

$comboPromise = Amp\all([$promise1, $promise2]);
list($result1, $result2) = $comboPromise->wait();

EDIT

If your push script is push.php and your other script is api.php:

<?php 

function processAll() {
    exec('nohup /usr/bin/api.php > /dev/null 2>&1 &');
    exec('nohup /usr/bin/push.php > /dev/null 2>&1 &');
}

EDIT 2

You'll need to be comfortable writing command line scripts. Ensure you can run them yourself on the command line before you try to cram them into exec() - you may need to prefix the command with php, ie nohup /usr/bin/php script.php (again, you can run this in command line to test with).

Community
  • 1
  • 1
Christian
  • 3,917
  • 2
  • 23
  • 40
  • i can't use threading right now because of server and php limitations. i have to take down all apis and install few things to enable safe threading. i have checked the first one. /dev/null 2>&1 &'); ?> how would i run my function inside this exec – Muhammad Umar Oct 09 '15 at 03:10
  • I've just added some pseudo code at the bottom of my answer. Have a go and report back. – Christian Oct 09 '15 at 03:13
  • Also just added more info about CLI. – Christian Oct 09 '15 at 03:18
  • i can't install pthreads for now. however other option seems good. the main problem is passing arguments inside the function. i can call push.php but how will i pass array to it to send push to specific users :) – Muhammad Umar Oct 09 '15 at 03:18
  • its gonna take me time to test . once i am done i will update u – Muhammad Umar Oct 09 '15 at 03:50