GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing taskiq-python.github.io — Getting started Getting started9/7/22 Getting startedInstallationYou can install taskiq from pypi or directly from git using pip: pypi git After installation of the core library, you need to find the broker that fits you. You can do it using PyPI search. Cool tip! We highly recommend taskiq-aio-pika or taskiq-nats as the broker and taskiq-redis as the result backend for production use. Running tasksNow you need to create a python module with broker declaration. It's just a plain python file with the variable of your broker. For this particular example, I'm going to use the Important note The InMemoryBroker doesn't send any data over the network, and you cannot use this broker in a real-world scenario, but it's still useful for local development if you do not want to set up a taskiq worker. And that's it. Now let's add some tasks and the main function. You can add tasks in separate modules. You can find more information about that further. Also, we call the Cool warning! Calling the If you run this code, you will get this in your terminal: Ok, the code of the task execution is a little bit fancier than an ordinary function call, but it's still relatively simple to understand. To send a task to the broker, you need to call the You can get more information about taskiq types, CLI and internal structure in the "Architecture overview" section. Distributed runNow let's change InMemoryBroker to some distributed broker instead. In this example we are going to use broker that works with RabbitMQ. At first we must install the taskiq-aio-pika lib. After the installation, replace the broker we defined earlier with the broker from the Also, the AioPika broker requires calling startup before using it. Add this line at the beginning of the main function. That's all you need to do. Complete codeLet's run the worker process. First of all, we need RabbitMQ up and running. I highly recommend you use docker. linux|macos windows Now we need to start worker process by running taskiq cli command. You can get more info about the CLI in the CLI section. After the worker is up, we can run our script as an ordinary python file and see how the worker executes tasks. But the printed result value is not correct. That happens because we didn't provide any result backend that can store results of task execution. To store results, we can use the taskiq-redis library. After the installation, add a new result backend to the broker. Now we need to start redis. linux|macos windows Complete codeLet's run taskiq once again. The command is the same. Now, if we run this file with python, we can get the correct results with a valid execution time. Continue reading to get more information about taskiq internals. TimeoutsIf you want to restrict the amount of time you want to run a task, just add timeout label to the task. You can do it either with decorator or when calling the task. decorator when calling Cool alert We use run_in_executor method to run sync functions. Timeouts will raise a TimeoutError, but synchronous function may not stop from execution. This is a constraint of python. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |