Tzahi Ben Arzi
11/30/2023, 8:26 PMMarvin
11/30/2023, 8:26 PMconcurrency
context manager. For example, if you want to limit the number of process_data
tasks to five at any one time, you can request five slots on the data-processing
concurrency limit.
Here is an example of how you can use the concurrency
context manager:
with concurrency("database", occupy=1):
connection = psycopg2.connect("<connection_string>")
cursor = connection.cursor()
cursor.execute(query)
result = cursor.fetchall()
connection.close()
return result
@flow
def my_flow():
queries = ["SELECT * FROM table1", "SELECT * FROM table2", "SELECT * FROM table3"]
for query in queries:
database_query.submit(query)
if __name__ == "__main__":
my_flow()
In this example, the concurrency
context manager is used to ensure that we never exceed the maximum number of database connections.
For more details, you can refer to the Prefect documentation.Tzahi Ben Arzi
11/30/2023, 9:27 PMMarvin
11/30/2023, 9:27 PM