Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Tasks
3.
Task Lifecycle
4.
Queue Task
5.
Task Status
6.
Frequently Asked Questions
6.1.
What is pylon framework?
6.2.
Which is better, web2py or Django?
6.3.
What are Django and Flask?
6.4.
What is the web2py framework?
6.5.
What is Falcon API?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Tasks In Web2py

Author Rajat Agrawal
0 upvote

Introduction

Web2py is a Python-based, accessible, and open-source web framework enabling  Agile development of database-driven online applications. It is a full-stack framework that includes all the elements a developer needs to create a fully functional web application.

In this blog, we will discuss how to schedule Tasks in Web2py. 

Intro to Web2py

Tasks

Tasks may be scheduled programmatically or via appadmin. In fact, a task is simply scheduled by adding an item to the table "scheduler task," which can be accessed via appadmin:-

http://127.0.0.1:8000/myapp/appadmin/insert/db/scheduler_task


The fields in this table have clear meanings. The values to be supplied to the task in JSON format are stored in the "args" and "vars" columns. In the context of the preceding "task_add", an example of "args" and "vars" may be:

args = []
vars = {'a': 10, 'b': 15, 'c': 20}


The tasks are organized in the task_scheduler table.

To add tasks via the API, use

scheduler.queue_task('mytask', ...)

Let’s learn about Task Lifecycle.

Task Lifecycle

All the tasks follow a lifecycle as shown below.

Task lifecycle


A task is by default in the QUEUED status when it is sent to the scheduler. Use the start_time parameter (default = now) if you require it to run at a later time.

You can provide a stop_time (default = None) for a job if you need to ensure that it is not carried out after a specific time. Your job will be marked as EXPIRED if no worker picks it up before stop_time. Tasks that are picked up before stop_time or without a stop_time defined are ASSIGNED to a worker. The status of a task is changed to RUNNING when a worker picks it up.

RUNNING tasks may end up:

Results in running tasks

The value of start_time and stop_time should be set as a Datetime object. As an example, if you wanted to set up mytask to run 45 seconds from now, you would do as follows:-

from datetime import timedelta as timed
scheduler.queue_task('mytask', start_time=request.now + timed(seconds=45))

Queue Task

The queue_task method allows you to queue the task to be executed by a worker. 

The queue_task method is shown below:-

scheduler.queue_task(function,
                    pargs=[],
                    pvars={},
                    start_time=now,  # datetime
                    stop_time=None,  # datetime
                    timeout = 60,  # seconds
                    prevent_drift=False,
                    period=60,  # seconds
                    immediate=False,
                    repeats=1)


It takes the above-mentioned arguments and returns a row as a result. Some of the important parameters are described below.

Important parameters of queue_task method

Task Status

task_status is used to query the scheduler about tasks. It returns a single Row object, for the most recent queued task matching the criteria.

Syntax:

scheduler.task_status(ref, output=False)


The argument ref can be

  • integer: The lookup will be done by scheduler_task.id.
  • string:  The lookup will be done by scheduler_task.uuid.
  • query: The lookup as you wish (as in db.scheduler_task.task_name == 'test').

output = True fetches the scheduler_run record.

Frequently Asked Questions

What is pylon framework?

Python-based Pylons Framework is an open-source Web application framework. The Web Server Gateway Interface standard is heavily utilized to encourage reusability and divide functionality into independent modules.

Which is better, web2py or Django?

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 significantly more straightforward syntax.

What are Django and Flask?

While Django is a high-level web framework for Python, Flask is a micro-framework. As a result, the flask is considerably simpler to comprehend and learn. Both are open-source platforms. However, the flask is more commonly used for lightweight apps than Django. Both are utilized differently and for various purposes.

What is the web2py framework?

Web2py, which is written in Python and programmable in Python, is described as a free, open-source online framework for agile development that involves database-driven web applications.

What is Falcon API?

Falcon is a lightning-quick, lightweight Python web API framework for creating reliable app backends and microservices. The framework performs admirably with both gevent/meinheld and asyncio (ASGI) (WSGI).

Conclusion

In this article, we have extensively discussed Tasks in Web2py, we also learned about Tasks, Task Lifecycle, Task Queue, and Task Status. I hope you enjoyed reading this article on Tasks in Web2py. If you want to learn more, check out our articles on What Is Web2Py?Why To Use Web2py?Postbacks and Internationalization in web2py, and  XML in Web2py.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass