Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Example 1👨‍💻
Example 2👨‍💻
Example 3👨‍💻
Task Queues
Frequently Asked Questions
Does Windows support web2py Cron?
What purpose does @controller provide in web2py?
How can I access my web2py server?
What is my web2py server's login procedure?
In web2py, how do you create a database?
Last Updated: Mar 27, 2024

Cron and Task Queues in Web2py

Author Mayank Goyal
0 upvote
Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


CRON in web2py allows the task to be executed at intervals. Every program has a CRON file that lists all of its features. Scheduler By establishing the priority, the built-in scheduler assists in conducting the jobs in the background. It offers a method for establishing, planning, and changing tasks.

Let us learn

Now, without further, let's move into the topic of discussion.



Applications can execute tasks at predetermined times in a platform-independent manner using the web2py cron. A crontab file for each program defines the cron functionality:


Before web2py 2.1.1, cron could be turned off with the -N command-line option. It was enabled by default. Since version 2.1.1, cron is default turned off and can be turned on using the -Y option. This change was made to encourage users to use the new scheduler instead of the cron system, which may hurt performance.

Each application can have its unique cron setting, and changes to the cron configuration can be made within web2py without impacting the host OS.

Example 1👨‍💻

0-59/1  *  *  *  *  root python /path/to/python/
30      3  *  *  *  root *applications/admin/cron/
*/30    *  *  *  *  root **applications/admin/cron/
@reboot root    *mycontroller/myfunction
@hourly root    *applications/admin/cron/

The final two lines of this example add more web2py capability using modifications to standard cron syntax.

A real file named "applications/admin/cron/expire" comes with the admin app. It looks for sessions that have expired and delete them. This task is done every hour by "applications/admin/cron/crontab."

The task or script will be run in the web2py environment if preceded with an asterisk (*) and ends This indicates that you will have access to all controllers and models. The models won't run if there are two asterisks (**). This calling method is advised because it has lower overhead and prevents potential locking issues. You should be aware that scripts and functions run in the web2py environment must manually call db.commit() at the end of the function to avoid the transaction being rolled back.

Make sure your web2py code runs without errors before setting it up as a cron task because you probably won't be able to detect those errors when running from cron. After all, web2py does not generate tickets or meaningful tracebacks in shell mode, which is how cron is handled. Additionally, use models with caution. Although the execution occurs in a separate process, database locks must be considered to prevent pages from waiting for cron tasks that could be blocking the database. If you don't need to use the database in your cron task, use the ** syntax. 

Another option is invoking a controller function, in which a path is unnecessary. The invoking application's Controller and function will be used. Pay close attention to the warnings mentioned above.

Example 2👨‍💻

*/30  *  *  *  *  root *mycontroller/myfunction

If @reboot is entered in the crontab file's first field, the specified task will only be run once upon web2py startup. You can utilize this capability if you wish to pre-cache, verify, or initialize data for an application on web2py startup. Cron tasks are conducted concurrently with the application; as a result, you should design checks to reflect that the application won't be ready to service requests until the cron task has been completed.

Example 3👨‍💻

@reboot  root *mycontroller/myfunction

Depending on how you invoke it, there are four different ways to use web2py cron:

  Type                                       Description
Soft Cron Available in all execution modes is soft cron.
Hard Cron When utilizing the built-in web server, hard cron is accessible (directly or through Apache mod proxy).
External Cron If you have access to the system's cron service, you can use an external cron.
No Cron  

If you use the built-in web server, hard cron is the default; otherwise, soft cron is the default. If you are utilizing CGI, FastCGI, or WSGI, soft cron is the default approach (but note that soft cron is not enabled by default in the standard file provided with web2py).

Your tasks will be carried out by soft cron on the first call (page load) to web2py following the time set in the crontab, but only after processing the page, so the user won't notice any delay. Depending on how much traffic the site receives, it is unsure exactly when the task will be completed. Additionally, if the web server has a page load timeout specified, the cron process may be halted. If these restrictions are intolerable, look into external cron. Although soft cron is a viable last resort, it should be avoided if your web server supports alternative cron techniques.

If you use the built-in web server (directly or through Apache mod proxy), hard cron is the default. Hard cron, unlike soft cron, has no run-time or execution time precision restrictions because it is executed in a parallel thread.

The system cron facilities must be used if you want to use external cron, which is not the default in any situation. It operates in a parallel process. Therefore none of soft cron's restrictions applies. The suggested method for utilizing cron with WSGI or FastCGI is as described above.

An example of a line to add to the system crontab, which is often found in /etc/crontab:

0-59/1 * * * * web2py cd /var/www/web2py/ && python -J -C -D 1 >> /tmp/cron.output 2>&1

Make careful to specify either -J (or —cronjob, which is the same) when using an external cron so that web2py is aware that the task is being carried out by cron. Internally, Web2py sets this using soft and hard cron.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Task Queues

Cron is handy for scheduling jobs to run at regular intervals, but it isn't necessarily the ideal option for background operations. Web2py offers the ability to execute any Python script as though it were inside a controller for this reason:

python -S app -M -R applications/app/private/ -A a b c

where "" is launched as an "app" by web2py when "-S app" is used, "-M" instructs web2py to run models, and "-A a b c" specifies optional command-line arguments:

sys.argv = ['applications/app/private/', 'a', 'b', 'c']

to ""

You must ensure that only one instance of this background process runs simultaneously. Thus cron should not be used to run it (except cron @reboot). When using cron, it is conceivable for a process to start at cron iteration 1, not finish by cron iteration 2, and then start over and over again, jamming the mail server.

Frequently Asked Questions

Does Windows support web2py Cron?

Windows users can also use the web2py cron. If you need tasks to be completed in the background at certain periods and they take less time than the time between two calls, you should use web2py cron. Although several jobs may run simultaneously, and each one runs in its process, you have no control over how many tasks are active at any given time.

What purpose does @controller provide in web2py?

The Controller manages user interaction and is a component of the application. Controllers can read information from views, manage user input, and send information to a particular model. A built-in feature of web2py allows you to control cookies and sessions.

How can I access my web2py server?

You need to see the web2py application by default when you visit the website: You can log in using the password you just chose while launching the server if you click the "Administrative Interface" button. When finished, you need to be directed to the admin interface: Three folders may be seen on the left.

What is my web2py server's login procedure?

You should see the default web2py application when you first access the website: You can log in by selecting the "Administrative Interface" button and entering the password you just selected when starting the server. The admin interface should be displayed when finished: Three folders are visible on the left.

In web2py, how do you create a database?

The web2py application code is agnostic of any database engine because the database queries can be written in raw SQL or utilizing the web2py Database Abstraction Layer. The model communicates with the Controller and creates the database connection with the database.


In this article, we learned about Cron and task queues in web2py with the help of some articles. That's the end of the article. I hope you all like it.

To learn more about web2py, you can refer to Introduction to an image blogInstallation and startup in web2pyand Use of web2py

That's the end of the article. I hope you all like this article. 

Do upvote our blogs if you find them helpful and engaging!

Happy Learning, Ninjas!

Previous article
URL Rewrite In Web2py
Next article
Third Party Modules In Web2py
Live masterclass