Support » Developing with WordPress » Starting a ‘background’ thread in WP PHP

  • I have a service that requires I do somewhat lengthy word in the ‘background’ at a users request. I don’t want to use the ‘main’ thread that is calling my plugin functions, I want to start a thread and have it do the execution. I see there are some CRON type services for WP. But I was wondering if there are any thread services in WP for PHP?

    Thanks in advance

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t think WP does anything with process control, but of course PHP has several process control modules. The question then is which ones are available? The only one in my PHP installation is POSIX, but it would not be available on Windows platforms. If you’re developing a plugin for general distribution, which modules are available will be a problem for you.

    If this is just for your own site’s use, determine which modules are available and go with that.

    Unrelated, but since you mentioned CRON, WP Cron is not a true CRON service. It’s principle advantage is it is independent of any server configuration. But for various reasons it is not always totally reliable. If you need reliable CRON service, use server CRON and not WP Cron.

    Thread Starter kspambot

    (@kspambot)

    My WP plugin is not for distribution, it is to solve my particular problem in WP. I assume that my WP site is always running on a Linux server, so maybe a PHP process control module that implements pthreads would be good enough. Thanks for the pointer on CRON.

    Thread Starter kspambot

    (@kspambot)

    Regarding your statement ” I don’t think WP does anything with process control, but of course PHP has several process control modules. ” This leads me to deduce that PHP when used with WordPress is exactly or very similiar to PHP when executed stand alone (without WordPress). I am new to WP/PHP that is why I want to clarify my assumption.

    Thanks

    K-

    Thread Starter kspambot

    (@kspambot)

    Looks like to use other threads, that PHP has to be build with ZTS (Thread Safe). I notice that the usual WP installation doesn’t have a PHP with ZTS. So is it a bad idea to write Multi threaded php?

    Moderator bcworkz

    (@bcworkz)

    For multi-threading you need some kind of process control module as part of the server’s PHP installation. WP uses the PHP that’s available on the server, it doesn’t have its own PHP engine. Since process control availability is unpredictable, it’d explain why WP doesn’t try to do multi-threading.

    Unless you can configure your own PHP installation, or get your host to alter it for you, you’re stuck with what’s already available. If you can work with what you have, since it’s for a custom one site application, you can do whatever is necessary to achieve your goals. It’s not whether it’s a bad idea or not, it’s whether it’s even possible with what you have to work with.

    To see how your PHP installation is configured, place the following into a new .php file saved in your site’s public root folder:

    <?php
    phpinfo();

    Then request the file from your browser. You’ll see a lengthy output of all of your PHP’s configuration parameters. Determine which process control modules are available, if any.

    One other option, which will be easier and probably work better, is to make the call to the second request as an AJAX call from your site. That will let it be processed by it’s own server threads without you needing to do anything, and will work pretty much anywhere that you host your site.

    Also rememeber that multi-threading in PHP is probably not what you want to do unless there’s something very specific that’s needed. The reason for that is that as PHP is really only single-threaded, all of the processing is done and output so anything that’s in a different thread may or may not be ready when it’s needed. Using AJAX will let you do the processing and update the sites HTML code using JavaScript when it’s all done.

    This will all depend on exactly what you are trying to achieve with this whole system. What are you trying to do?

    Thread Starter kspambot

    (@kspambot)

    OK. I have a long python script that needs to run to service the user. I think I will make the python script listen on a socket and get requests from WP/PHP. I assume that PHP has a sockets interface.

    I appreciate the feedback from all the responders.

    K-

    Moderator bcworkz

    (@bcworkz)

    I assume that PHP has a sockets interface.

    See https://www.php.net/manual/en/book.sockets.php

    It’s a PHP extension that needs to be enabled, but AFAIK it commonly is enabled. If you plan to communicate over HTTP(S), you could use WP_Http class for communication.
    https://developer.wordpress.org/reference/classes/wp_http/

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.