Assuming you need to run several, possibly simultaneous, instances of the background task, the solution is a task queue. I heard good things about Celery and RabbitMQ if you are looking for third-party options, and web2py includes its own task queue , which may be sufficient for your needs.
Using any tool, you define a function that encapsulates the operation that you want the background process to perform. Then translate the work queues of tasks online. The web2py manual and forums indicate that this can be done using the @reboot operator on the web2py cron system, which runs whenever the web server starts. Perhaps there are other ways to get started if this is unsatisfactory.
In your controller, you will enter the task into the task queue, passing any necessary parameters as input for the function (the background function will not work in the same environment as the controller, so it will not have access to the session, database, etc. (unless you explicitly pass the corresponding values ββto the task function).
Now, to get the output of the background operation to the user. When you insert a task into the task queue, you must return a unique identifier for the task. Then you must implement the controller logic (either something waiting for an AJAX call, or a page that continues to refresh until the task completes), which calls the task queue API to check the status of the specified task. If the task status is "completed", return the data to the user. If not, keep waiting.
spiffytech
source share