-6

How to add pthreads to MAMP PRO PHP 5.6.10 on MAC OS X 10.11?

How to install it and recompile PHP to use --enable-maintainer-zts ?

Tom
  • 1,203
  • 3
  • 15
  • 35
  • http://stackoverflow.com/questions/22232865/how-to-enable-pthreads-on-mamp – Shivam Mathur Sep 03 '16 at 22:39
  • 1
    Yes, I have read it but I stucked in recompiling PHP and I didn't find any tutorial that shows how to do this without any bugs. – Tom Sep 04 '16 at 08:32
  • @Tom, what OS do you have on server? – Spell Sep 06 '16 at 20:29
  • @Spell I want to use it locally on my MAC OS X 10.11.6. I use apache server from MAMP PRO 3.5 with PHP 5.6.10 (eventually MAMP PRO 4 with PHP 5.6.26). This is for development purposes. – Tom Sep 07 '16 at 10:33
  • You can use a manual from this page http://www.smddzcy.com/2016/01/installing-configuring-php7-zts-on-os-x/ – Spell Sep 07 '16 at 18:32
  • Use brew to install PHP. Much easier, here's a gist from someone that has installed php5.6 with pthreads from brew. Hope that helps. https://gist.github.com/bipinu/489306e6dc6a693e712c – Robert Wade Sep 09 '16 at 10:50
  • If you want to use pthreads in web server context - you can't, you don't need it and you will never need it (I know I sound like a dick, but it's true). pthreads are available for CLI SAPI only. Now, if you can share what the problem you're having *is*, we could come up with a different solution that doesn't involve threads in web server context. On top of everything, you're using Apache.. which is a fine recipe for disaster. – N.B. Sep 09 '16 at 18:03
  • @N.B. I have a SaaS solution with web backend and mobile app. Mobile app has chat done without any Sockets, jest normal REST API. Sending a message takes about 1500ms where DB queries takes no more than 10ms, more than 500ms for Autload, more than 700ms for connecting to Firebase Cloud Messaging with Curl (actually 200 - 1500ms, it depends). I need to speed up autoloading. In Symfony controller I do the magic, returns response (required) and in the meantime send PushNotification with FCM via curl in a separate thread. So I return response to the app and separate thread waits to connect to FCM. – Tom Sep 10 '16 at 06:56
  • @Tom - threads won't help you here AT ALL. Changing from `mod_php` to `php-fpm` would. You don't even have to change Apache for nginx because Apache can speak FastCGI. `php-fpm` has this great function called `fastcgi_finish_request` which sends a response to client and does some work in background after the function call. It's just a suggestion to get you in the right direction. Even if you managed to install the pthreads extension (it's disabled for that SAPI), autoloading in thread won't help at all. – N.B. Sep 10 '16 at 08:09
  • @N.B. I wouldn't autoload in separate thread, just send push notifications with it. But I will try your solution, cause we have php-fpm on server. Also what I have learned is that Symfony framework uses 'fastcgi_finish_request' to send response and you can do other jobs later if server has php-fpm Could you please right your comment as an answer? Cause I will leave Threads (which are cumbersome to use) and do php-fpm instead. – Tom Sep 10 '16 at 10:23

1 Answers1

4

This question was an XY problem and it didn't deserve so many downvotes.

The real problem

how to perform a long running task while returning a response to the client so they don't feel the app is slow

The solution

Using PHP-FPM and function fastcgi_finish_request

Example

<?php

// This is the output that we return to browser.
echo 'Your request has been accepted';

// "hang up" and send the data to the web server 
fastcgi_finish_request();

// Now perform the long running task (this gets executed in background, sort of)
$i = 1;

while($i--)
{
    // We're wasting some CPU cycles to simulate "work" (don't use this in real app)
    sleep(1);
}

Potential problems

Even though you can return a response to the client, a PHP-FPM child process will still be occupied until the task is completed. Since by default you don't boot many of these processes, you can quickly run out of processing power.

Use with care

Alternative solution

Alternative would be to use a job queue based approach. A long running process (daemon written in PHP or something done with Node.js) reads a queue of tasks (from database / nosql, whichever you're familiar with) and executes tasks.

Frontend-facing PHP simply fills the queue and notifies the client that job has been queued without actually doing the work.

That way you can have multiple workers performing long-running tasks by reading from same job queue source. Even if one dies or something else bad happens, you can always resume the work by restarting the worker process.

N.B.
  • 13,688
  • 3
  • 45
  • 55
  • Thank you @N.B. for your help and this answer. Indeed you helped me to realize how many other things can be done with this solution. Indeed Symfony uses fastcgi_finish_request(); if PHP-FPM is available (for isntance to send email after response has been sent, not to block the client). Using threads is cumbersome so this solution is much better. I got more from this answer than if someone would give me a solution to install pthreads. – Tom Sep 10 '16 at 16:40
  • Bear in mind that there could be some problems, hence the alternative solution. I'd say that my answer is just a quick fix at this point, but it's better than nothing. Threading is a complicated beast and it can get out of hand pretty quickly. Using an async framework might help or shifting the chat app to a node.js based approach (if you have the luxury to be able to use it). Good luck with your application! – N.B. Sep 10 '16 at 16:55