Table of contents
1.
Introduction📄
2.
Web2py📄
3.
Web2py Scheduler
4.
Parameters
5.
Scheduler Deployment
6.
Scheduler Signature
7.
Frequently Asked Questions
7.1.
What purposes serve web2py?
7.2.
How to schedule a task in web2py?
7.3.
Describe the web2py framework.
7.4.
What's the full scheduler signature?
7.5.
Does Python 3 support web2py?
7.6.
Which is better, Django or web2py?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Parameter and Scheduler in Web2py

Author Mayank Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction📄

If a python programmer wants to create a website using a fast, open-source, full-stack framework, then web2py is the correct framework that fits their bucket. Web2py helps us develop fast, scalable, and portable database-driven web-based applications. 

Web2py

Today, we will briefly look at adding images to the web2py and how to add Authentication to an Image Blog in Web2py.

Web2py📄

Web2py is an open-source, free full-stack framework written in Python that helps rapidly develop fast, scalable, secure, and portable database-driven web-based applications.Web2Py is a full-stack framework as it contains all the tools, components, and APIs essential to building a fully functional web application.Web2Py is compatible with both versions of Python, i.e., python 2. X and Python 3.X.Architecture followed by Web2py is a Modern View Controller followed by many famous web frameworks like Django and Ruby on Rails.

Web2py is built by keeping security in mind and is its very first concern for it. As a Back-End framework, web2py comes with a built-in Data Abstraction Layer that helps developers communicate with different SQL databases such as SQLite, PostgreSQL, MySQL, MSSQL, Oracle, IBM DB2, etc.

Let us learn Web2py

Some of the many features of Web2py are its web-based IDE, popular frameworks that inspire it, its foundation on MVC architecture and its high compatibility, and web-based IDE.

Web2py Scheduler

The built-in scheduler is the most popular web2py option for running tasks in the background (and hence separate from the webserver process).

The stable API consists of these functions:

disable()

resume()

terminate()

kill()

queue_task()

task_status()

stop_task()

With a few differences, the web2py scheduler functions similarly to the task queue described in the preceding section:

It offers a standardized method for defining, planning, and managing projects.

There are several worker processes, not just one background process.

Since both the state of the tasks and the state of the worker nodes are maintained in the database, it is possible to monitor their performance.

Although it is not described here, it functions without web2py.

 

A job in the scheduler is merely a function specified in a model (or in a module and imported by a model). For instance:

def task_add(a, b):
    return a + b


All global variables defined in models, including database connections, are visible to controllers because tasks are constantly run in the same context (db). Because tasks are not connected to an HTTP request and don't have a request.env, they differ from controller actions. Additionally, tasks have access to a different environmental variable not found in typical requests: W2P_TASK. W2P_TASK.id holds the scheduler_task.id and W2P_TASK.uuid the scheduler_task.uuid field of the task that is running.

The Scheduler class must be instantiated in a model to enable the scheduler. The suggested method for adding a scheduler to your app is to define your function in a model file called scheduler.py. You can add the following code to the model after the functions:

from gluon.scheduler import Scheduler
scheduler = Scheduler(db)

Parameters

The scheduler's database to communicate with the workers must be the first argument of the Scheduler class. This can be the app's database or a different, special database, perhaps one that is shared by several apps. To keep your app responsive when using SQLite, it is advised to use a different database than the one used by the app. The only thing left to do is start the workers once the tasks have been defined and the scheduler has been created. There are various ways to accomplish that:

python web2py.py -K demoapp


Launches a worker for our demoapp. Simply supplying demoapp,demoapp will launch multiple workers for the same application. You can also pass the group names argument to the model's collection of names.

python web2py.py -K demoapp:group1:group2,myotherapp:group1


From the default window of Web2py, you can start/stop the workers if you have a model named scheduler.py (the one you use to set the IP address and the port).

Scheduler Deployment

One more useful improvement: if you use the embedded web server, you can launch both the web server and the scheduler with just one line of code (assuming you don't want the web2py window to appear; otherwise, use the "Schedulers" menu instead).

Scheduler Deployment
python web2py.py -a yourpass -K demoapp -X


You can add a -X after the standard arguments (-i, -p, and -a, in this case, prevent the window from appearing) and any program to the -K parameter. The web server will function in tandem with the scheduler.

Scheduler Signature

The scheduler's full signature is:

Scheduler(db,
       tasks=None,
      migrate=True,
      worker_name=None,
      group_names=None,
      heartbeat=HEARTBEAT,
      max_empty_runs=0,
      discard_results=False,
      utc_time=False)

 

Now, let's see each parameter in detail:

Parameters and description

Frequently Asked Questions

What purposes serve web2py?

Python dynamic web content programming is made possible via Web2py. Web2py is made to make laborious web development jobs easier, such as creating web forms from scratch, while a web developer can still do it if necessary.

How to schedule a task in web2py?

To schedule a job, you must first insert a record in the db—scheduler task table. You can do this programmatically using the scheduler. Queue task method (which you use in your daily periodic task action) or using any of the standard record-inserting techniques, such as appadmin.

Describe the web2py framework.

Web2py, written in Python and programmable in Python, is a free, open-source online framework for agile development that involves database-driven web applications. It is a full-stack framework with every part a developer might require to create a fully functional web application.

What's the full scheduler signature?

Scheduler(db,
          tasks=None,
          migrate=True,
          worker_name=None,
          group_names=None,
          heartbeat=HEARTBEAT,
          max_empty_runs=0,
          discard_results=False,
          utc_time=False)

Does Python 3 support web2py?

On Python 2.7 and Python 3, web2py functions with CPython (the C implementation) and PyPy (Python written in Python).

Which is better, Django or web2py?

Due to its smaller size, simpler learning curve, and lack of project-level configuration files, web2py differs from Django. Compared to PHP-based frameworks and Java-based frameworks, web2py has a much cleaner syntax. This makes applications easier to read, maintain, and develop.

Conclusion

In this article, we will learn about web2py, its scheduling, the different parameters involved, scheduler deployment, and its full signature with codes. That's the end of the article. I hope you all find it useful. 

Be curious to learn code!

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!

Thankyou image
Live masterclass