taskiq-python / taskiq

Distributed task queue with full async support
MIT License
689 stars 44 forks source link

must not use a script's none-relative path as entrypoint to run #329

Open ryanrain2016 opened 1 month ago

ryanrain2016 commented 1 month ago

taskiq_demo.py如下

import asyncio

from taskiq_redis import ListQueueBroker, RedisAsyncResultBackend

redis_dsn = 'xxxx'
broker = ListQueueBroker(redis_dsn, queue_name='taskiq:req').with_result_backend(
    RedisAsyncResultBackend(redis_dsn)
)

@broker.task
async def add_one(value: int) -> int:
    print(value, '#' * 20)
    return value + 1

async def main() -> None:
    # Never forget to call startup in the beginning.
    await broker.startup()
    # Send the task to the broker.
    task = await add_one.kiq(1)
    # Wait for the result.
    result = await task.wait_result(timeout=5)
    print(f"Task execution took: {result.execution_time} seconds.")
    if not result.is_err:
        print(f"Returned value: {result.return_value}")
    else:
        print("Error found while executing task.")
    await broker.shutdown()
if __name__ == "__main__":
    asyncio.run(main())

run the command to start worker

taskiq worker taskiq_demo:broker

run the command to run the program

python ./taskiq_demo.py

then i get a output task "..taskiq_demo:add_one" is not found. Maybe you forgot to import it? when I stop the taskiq window and run the script again, I get a task with name ..taskiq_demo:add_one in my redis. code in taskiq/abc/broker.py(line 317-322) produce the problem

s3rius commented 3 weeks ago

So, it happens because the way tasknames generated. You can fix this issue by explicitly defining taskname in the decorator.

@broker.task(task_name="add_one")
async def add_one(value: int) -> int:
    print(value, '#' * 20)
    return value + 1