Parallel Task Execution
Learn how to execute tasks sequentially and in parallel with Upsonic
Overview
Upsonic provides flexible mechanisms for task execution, allowing you to run tasks both sequentially and in parallel, with synchronous and asynchronous options. This flexibility enables efficient task processing tailored to your specific requirements, whether you need ordered execution, concurrent processing, or non-blocking operations.
Task Execution Methods
Upsonic offers four primary methods for task execution:
- Sequential Synchronous - Execute tasks one after another, waiting for each to complete. Ideal for workflows where tasks depend on previous results or when order matters.
- Sequential Asynchronous - Queue tasks for execution without blocking the main thread. Perfect for background processing where you want to continue executing your main program while tasks complete in the background.
- Parallel Synchronous - Execute multiple tasks concurrently, waiting for all to complete. Best for CPU-intensive independent operations where you need maximum throughput but still need all results before proceeding.
- Parallel Asynchronous - Execute multiple tasks concurrently without blocking the main thread. Excellent for responsive applications that need to handle multiple operations simultaneously without freezing the user interface.
Creating Tasks and Agents
Before executing tasks, you need to define your tasks and create an agent to process them:
Sequential Task Execution
Synchronous Sequential Execution
Execute tasks one after another, waiting for each task to complete before moving to the next:
This method ensures tasks are executed in a specific order, with each task fully completing before the next begins. Use this approach when tasks must execute in a particular sequence, such as data preparation followed by analysis, or when subsequent tasks depend on the results of previous ones.
Asynchronous Sequential Execution
Queue tasks for execution without blocking the main thread:
The print_do_async
method returns immediately, allowing your code to continue execution while tasks are processed in the background.
This execution model is particularly useful for user interfaces and web applications where you need to maintain responsiveness while processing tasks in a defined order. It’s also valuable for long-running operations that shouldn’t block the main application flow.
Parallel Task Execution
Synchronous Parallel Execution
Execute multiple tasks concurrently and wait for all tasks to complete:
This method significantly improves performance when handling independent tasks by utilizing multiple processing resources simultaneously, but it will block until all tasks are complete. Use this when you need to process a batch of operations as quickly as possible and your program logic requires all results before continuing execution, such as processing multiple data files for a consolidated report.
Asynchronous Parallel Execution
Execute multiple tasks concurrently without blocking the main thread:
When using asynchronous methods, make sure your application handles the continuation of execution appropriately, as it won’t wait for tasks to complete.
This approach provides maximum performance and responsiveness, making it ideal for applications that need to handle multiple independent operations without affecting the user experience. Common use cases include background data syncing, concurrent API calls to different services, or processing large batches of items with no interdependencies.
Performance Considerations
- Sequential Execution: Best for tasks that must be executed in a specific order or depend on each other’s results
- Parallel Execution: Ideal for independent tasks that can be processed simultaneously
- Synchronous Methods: Provide predictable execution flow but block until completion
- Asynchronous Methods: Improve responsiveness but require careful management of execution flow
Examples
Example 1: Data Processing Pipeline
When processing data through multiple transformations, sequential execution ensures each step completes before moving to the next:
Example 2: Web Scraping Multiple Sites
When scraping data from multiple websites, parallel execution maximizes efficiency:
Example 3: Responsive User Interface with Background Processing
Keep a user interface responsive while performing complex calculations:
Example 4: Microservice Orchestration
Coordinate calls to multiple microservices with different execution patterns: