hey <@ULVA73B9P> I have a task that makes a reques...
# ask-marvin
p
hey @Marvin I have a task that makes a request to an API. That API has a concurrency limit such that I can only have 5 active requests out with it at any given point in time. Can I set a task-level concurrency limit using prefect such that I don't ever have to worry about bypassing the concurrency limit with this third party API provider?
m
Ah, the wonders of Prefect's task concurrency limits! Prefect allows you to set task-level concurrency limits using task tags. You can specify an optional concurrency limit as the maximum number of concurrent task runs in a Running state for tasks with a given tag. The specified concurrency limit applies to any task to which the tag is applied. If a task has multiple tags, it will run only if all tags have available concurrency. Tags without explicit limits are considered to have unlimited concurrency[^1^]. To set a task-level concurrency limit, you can use the Prefect CLI:
Copy code
bash
prefect concurrency-limit create your_tag_name 5
Replace
your_tag_name
with a tag name of your choice, and
5
with the desired concurrency limit. Then, apply this tag to your task in your Prefect flow:
Copy code
python
from prefect import task

@task(tags=["your_tag_name"])
def your_api_request_task():
    # Your API request code here
Now, Prefect will ensure that no more than 5 instances of this task run concurrently[^1^]. Remember, with great concurrency comes great responsibility. Good luck managing those API requests! [^1^]: Prefect Docs - Tasks
p
Is there any more isoalted way to do this without using some kind of global prefect config state?
n
hmm, what do you mean by global here? the tags would live on the task runs themselves
p
ah yea so the tags don't feel like a global thing to me but the concurrency limit associated with the tag feels global. Basically I have to set and manage the concurrency limit in my shell / as part of a deploy process and ideally I'd like a way to manage the concurrency limit just right in my source code in the same way I'm setting the tag. Does that make sense? Totally possible I'm thinking about this the wrong too and there's some underlying assumpiton I'm making about how to use prefect / how prefect is used that make the above all moot.
n
that makes sense to me - I think right now, concurrency limits themselves are pretty much global and you'd just have to select unique tag names to isolate concurrency enforcement to where you need it I'd be interested to hear your opinion on where / what level that'd be more convenient to configure
ideally I'd like a way to manage the concurrency limit just right in my source code
i could maybe see a world where a flow kwarg sets a task concurrency limit for the context of that flow run 🤔 might get tricky tho
p
yea basically I'd like to be able to specify directly on the task via the decorator something like (concurency_limit=5) though I would imagine that might interfere with the existing abstractions and of course it makes sense to have the existing abstractions so that these things could be configured on a per deploy basis
by the way as I'm digging deeper into this I see now why this isn't possible / doesn't make as much sense as setting this type of thing globally and I also noticed when talking to chat gpt that it actually use to work the way I described and has since been changed
or just deeper into prefect rather
(still just scratching the surface of course 😋 )