Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Task Control Tutorial

Introduction

In CrewAI, task control is essential for managing and orchestrating various tasks that need to be performed. Task control includes starting, stopping, pausing, and resuming tasks. This tutorial will guide you through the concepts and operations related to task control.

Starting a Task

To start a task, you need to initialize it and then invoke the start method. Here is an example:

# Initialize the task
task = CrewAITask(name="data_processing")

# Start the task
task.start()
                

In this example, a task named "data_processing" is initialized and then started using the start method.

Stopping a Task

To stop a task, you simply call the stop method on the task object. Here is an example:

# Stop the task
task.stop()
                

Stopping a task will cease its execution immediately. This is useful when you need to terminate a task prematurely.

Pausing and Resuming a Task

Sometimes, you may need to pause a task and resume it later. CrewAI provides methods to handle this:

# Pause the task
task.pause()

# Resume the task
task.resume()
                

The pause method pauses the task, and the resume method resumes the execution from where it was paused.

Checking Task Status

To check the status of a task, you can use the status property. Here is an example:

# Check the status of the task
status = task.status
print(f"The current status of the task is: {status}")
                
The current status of the task is: running

The status property returns the current state of the task, which can be "running", "paused", "stopped", etc.

Handling Task Completion

When a task completes, you might want to perform some actions. You can handle task completion using callback functions. Here is an example:

def on_task_complete():
    print("Task has completed successfully.")

# Assign the callback function
task.on_complete(on_task_complete)
                

In this example, the on_task_complete function will be called when the task completes.

Summary

In this tutorial, we covered the basics of task control in CrewAI, including starting, stopping, pausing, resuming tasks, checking task status, and handling task completion. Understanding these operations will help you efficiently manage tasks in your CrewAI applications.