• Hello,

    I’m working on a plugin and my function is already working well on a limited amount of data but when using a large amount, it simple take too long, it insert post and postmeta via WP_Query on some Custom Type Post.

    So I would like to know which method to make a PHP task asynchronous is to be recommended, using the Cron of WordPress, Ajax or Something else?

    I would like not to be limited by timeouts, nor by the user leaving the page before the end of the execution. If possible stay as close as possible to the native behavior of WordPress / WooCommerce.

    I’m fine to use an external library but if I can avoid it I’d like as much.

    • This topic was modified 5 years, 1 month ago by adbelkader.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Using Cron or Ajax doesn’t alter the time available for execution on its own. Unless the server configuration has disabled the ability, you want to call code along the lines of
    @ini_set('max_execution_time', 120 ); //default 30

    This is best done as early as possible. Fortunately, plugins load fairly early. You can still break out the task as a Cron or Ajax call if you like, but the execution time must also be extended.

    Dion

    (@diondesigns)

    The max_execution_time setting has limited usefulness in web-facing PHP scripts because the timeout setting of the webserver (Apache/nginx) is typically in the 30-60 second range.

    While it is possible to run a web-facing PHP script “in the background” if PHP-FPM is being used, very few sites run PHP-FPM. (Likely because it can’t be set up via a one-click-install of PHP and a cookbook configuration…it requires actual knowledge.)

    Thread Starter adbelkader

    (@adbelkader)

    My problem is not the execution time set in the configuration. I just want to know the best practice to run a function that can possibly insert many many posts in the database.

    I am now using wp_schedule_single_event without changing the execution time and passing the data to my custom action and so now avoiding being interrupted by the user leaving the page during the execution.

    I can divide the data into smaller groups and run wp_schedule_single_event after wp_schedule_single_event until everything is done.

    For example, if I want to insert 1000 rows into posts, Is is good to insert them 5 by 5 by scheduling many event in queue ?

    • This reply was modified 5 years, 1 month ago by adbelkader.
    • This reply was modified 5 years, 1 month ago by adbelkader.
    • This reply was modified 5 years, 1 month ago by adbelkader.
    Dion

    (@diondesigns)

    WP_CRON works via POST requests, so unless it is set to run as a system CRON task, you still have the issue of the user leaving the page. Limiting the number of inserted posts will minimize this issue.

    But if your plugin must insert 1000 posts at a time, you may need to re-think the plugin design. That’s going to cause page load issues at best, and a crashed server at worst. Maybe you should consider looking at the code in wp_insert_post() and optimizing it for your specific application.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Asynchronize PHP function with long execution time’ is closed to new replies.