这是 Python/转换的用例吗?

发布于 2025-01-13 04:02:18 字数 628 浏览 3 评论 0原文

假设我的(asyncio)工作流程是这样的:

  1. A = aiohttp.get(url_a) (重试 n 次,每次重试超时)
  2. B = aiohttp.get(url_b) (重试 n 次,每次重试超时)
  3. 如果我得到了A& B-> 保存到数据库
  4. 如果我得到 A 但没有 B -> 则 ->如果我得到 B 但没有 A,则估算 B 并保存到数据库
  5. -> 如果我没有 A 也没有 B,则估算 A 并保存到数据库
  6. ->估算 A,估算 B,保存到数据库
  7. aiohttp.post(url_x) (A, B)

基本上,我试图获取 A 和 B,但我可能会以 4 种不同的情况结束:获取 A 和 B,获取 A 但不获取B,得到B但得不到A,什么也得不到。不管怎样,超时后,我必须知道我得到了什么,并继续输入缺失值(如果有)并保存到数据库。当我说插补时,它过于简单化了。实际上,我需要某种分支,检查输入并选择分支(类似于 if)。记录成功保存到数据库后,A和B应该发布到url_x。

这样的工作流程是(py)转换状态机的用例吗?如果是这样,是否有任何示例与我刚才描述的场景有点相似?作为状态机概念的新手,我什至不知道这里是什么状态以及什么是转换。

Let's say that my (asyncio) workflow is like this:

  1. A = aiohttp.get(url_a) (with n retries and timeout of each retry)
  2. B = aiohttp.get(url_b) (with n retries and timeout of each retry)
  3. if I got A & B -> save to the database
  4. if I got A but not B -> impute B and save to the database
  5. if I got B but not A -> impute A and save to the database
  6. if I haven't got A nor B -> impute A, impute B, save to the database
  7. aiohttp.post(url_x) (A, B)

Basically, I am trying to get A and B, but I could end in 4 different situations: get A and B, get A but not B, get B but not A and cannot get anything. Anyway, after timeout, I must know what I have got and continue with imputing missing values (if any) and saving to the database. When I say imputing, it is oversimplified. Actually, I need some kind of branching, checking inputs and choosing branch (something like if). After the record is successfully saved to the database, A and B should be posted to url_x.

Is such workflow an use case for (py)transitions state machine? If so, is there any example that is a bit alike the scenario I have just described? As a novice to the state machine concept, I don't even know what states are here and what are transitions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文