skip to Main Content

I have a long running task that is called in the controller, but I don’t want the user to have to wait for a long time for the page to load. What is the recommended way to call the function asynchronously?

I researched this topic, but there seems to be so many ways to do it, and all of them are quite complicated, or compromise on the security.

CI4 v4.3.5
PHP v8.2.21

I have tested the spatie/async package, which seemed the simplest to use, but it only lets you run multiple threads at the same time, but you have to wait for them to finish to continue.

I also tried calling my function using a spark command, but you still have to wait for it to finish.

I want to run 1 task separately so that the user can load their page faster.

2

Answers


  1. Handling long-running tasks asynchronously in CodeIgniter 4 (CI4) is to use a queue system. This approach is scalable, reliable, and allows you to run tasks in the background without blocking the main process.

    Summary with Example:

    1. Install a Queue Library (e.g., php-resque):

    Install php-resque or another queue library to manage background jobs.
    2. Add a Job to the Queue:

    In your controller, when a user triggers a long-running task, you add a job to the queue instead of processing it immediately.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search