Getting Started

This section covers the basics of getting Pynenc installed and running in your environment.

Installation

Installing Pynenc is straightforward and can be accomplished using pip. Simply run the following command in your terminal:

pip install pynenc

This command will download and install Pynenc along with its necessary dependencies. Once the installation is complete, you are ready to start using Pynenc in your Python projects.

For more detailed instructions and advanced installation options, refer to the Installation section in the Pynenc Documentation.

Quick Start

To get a quick feel of Pynenc, here’s a basic example of creating and executing a distributed task.

1. Define a Task

Create a file named tasks.py and define a simple addition task:

from pynenc import Pynenc

app = Pynenc()

@app.task
def add(x: int, y: int) -> int:
    add.logger.info(f"{add.task_id=} Adding {x} + {y}")
    return x + y

@app.direct_task
def direct_add(x: int, y: int) -> int:
    return x + y

2. Start Your Runner or Run Synchronously

Before executing the task, decide if you want to run it asynchronously with a runner or synchronously for testing or development purposes.

  • Asynchronously: Start a runner in a separate terminal or script:

    pynenc --app=tasks.app runner start
    

    Check out the basic_redis_example

  • Synchronously: For test or local demonstration, to try synchronous execution, you can set the environment variable:

    export PYNENC__DEV_MODE_FORCE_SYNC_TASKS=True
    

3. Execute the Task

result = add(1, 2).result
print(result)  # This will output the result of 1 + 2

print(direct_add(1, 2))  # Directly waits for result

4. Alternative: Use PynencBuilder for Setup

Pynenc also provides a flexible builder interface for configuring your app. Here’s how to configure an app using Redis and a MultiThreadRunner:

from pynenc.builder import PynencBuilder

app = (
    PynencBuilder()
    .redis(url="redis://localhost:6379")
    .multi_thread_runner()
    .build()
)

This creates a production-ready Pynenc app with Redis as the backend and a MultiThreadRunner for parallel execution.

You can pass this app to your task module and proceed just like before:

@app.task
def add(x: int, y: int) -> int:
    return x + y

For a more comprehensive guide on setting up and running this example, visit our Basic Redis Example on GitHub.

Important

Note that in Pynenc, tasks cannot be defined in Python modules intended to run as standalone scripts (where __name__ is set to "__main__"). This includes modules executed directly or using the python -m module command. To learn more about this limitation and its implications, refer to the Frequently Asked Questions or the detailed explanation in the Usage Guide.

Requirements

To effectively use Pynenc in a distributed system, the primary requirement is:

  • Redis: Currently, Pynenc requires a Redis server for distributed task management. Make sure Redis is installed and running in your environment.

Future Updates:

  • Development plans include extending support to additional databases and message queues to broaden Pynenc’s compatibility in various distributed systems.