您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. ## Guidelines for Database Connection Management in App Runner and Task Pipeline
  2. Due to the presence of tasks in App Runner that require long execution times, such as LLM generation and external requests, Flask-Sqlalchemy's strategy for database connection pooling is to allocate one connection (transaction) per request. This approach keeps a connection occupied even during non-DB tasks, leading to the inability to acquire new connections during high concurrency requests due to multiple long-running tasks.
  3. Therefore, the database operations in App Runner and Task Pipeline must ensure connections are closed immediately after use, and it's better to pass IDs rather than Model objects to avoid detach errors.
  4. Examples:
  5. 1. Creating a new record:
  6. ```python
  7. app = App(id=1)
  8. db.session.add(app)
  9. db.session.commit()
  10. db.session.refresh(app) # Retrieve table default values, like created_at, cached in the app object, won't affect after close
  11. # Handle non-long-running tasks or store the content of the App instance in memory (via variable assignment).
  12. db.session.close()
  13. return app.id
  14. ```
  15. 2. Fetching a record from the table:
  16. ```python
  17. app = db.session.query(App).filter(App.id == app_id).first()
  18. created_at = app.created_at
  19. db.session.close()
  20. # Handle tasks (include long-running).
  21. ```
  22. 3. Updating a table field:
  23. ```python
  24. app = db.session.query(App).filter(App.id == app_id).first()
  25. app.updated_at = time.utcnow()
  26. db.session.commit()
  27. db.session.close()
  28. return app_id
  29. ```