Hi Roman, thank you for great Beanie package.
Could you please point me to a direction how to use Runner class? It took me a while to figure out, that beside creating my own ExampleTask from Task, I need to pass that, as a model to init_beanie.
So now I'm able to push my tasks to Mongo DB and execute them with ExampleTask.queue() or await start() with Worker(task_classes=[ExampleTask]).
Next step is to use Runner, but in that case I get beanie.exceptions.CollectionWasNotInitialized.
My code looks like this:
import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from beanie import init_beanie
from beanie_batteries_queue import Task, Runner, Priority
class ExampleTask(Task):
data: str
async def run(self):
self.data = self.data.upper()
await self.save()
# This is an asynchronous example, so we will access it from an async function
async def example():
client = AsyncIOMotorClient("mongodb://localhost:27017")
await init_beanie(database=client.db_name,
document_models=[ExampleTask]
)
# Producer
task = ExampleTask(data="test", priority=Priority.HIGH)
await task.push()
# Consumer
runner = Runner(task_classes=[ExampleTask])
runner.start()
# Results in error:
# File "C:\Git\Todo-app_II\.venv\Lib\site-packages\beanie\odm\documents.py", line 1024, in get_settings
# raise CollectionWasNotInitialized
# beanie.exceptions.CollectionWasNotInitialized
if __name__ == "__main__":
asyncio.run(example())
Do you please have any suggestion how to fix this? Thank you
Hi Roman, thank you for great Beanie package. Could you please point me to a direction how to use Runner class? It took me a while to figure out, that beside creating my own ExampleTask from Task, I need to pass that, as a model to init_beanie. So now I'm able to push my tasks to Mongo DB and execute them with ExampleTask.queue() or await start() with Worker(task_classes=[ExampleTask]).
Next step is to use Runner, but in that case I get beanie.exceptions.CollectionWasNotInitialized.
My code looks like this:
Do you please have any suggestion how to fix this? Thank you