0

我了解 Tornado 中 corountine 的机制,但这是我无法弄清楚的问题,请帮帮我

考虑这个业务例程:这里有 5 个数据库操作

#operation 1
#use asynchronous method ,doesn't matter
#switch to other coroutine

#operation 2
#use asynchronous method ,doesn't matter
#switch to other coroutine


#operation 3
#use asynchronous method , but i'll use the result do 
#some process then update in operation 4
#switch to other coroutine


#operation 4
#use asynchronous method ,doesn't matter
#switch to other coroutine


#operation 5
#use asynchronous method ,doesn't matter
#switch to other coroutine

如您所见,我不希望任何其他相关的协程在每个操作 3 和操作 4 之间更新到同一张表或同一条记录,这会造成脏读和写。换句话说

#coroutine 1 operation 3
#coroutine 2 operation 3
#coroutine 1 operation 4
#coroutine 2 operation 4

不会被接受,正确的顺序应该是

#coroutine 1 operation 3
#coroutine 1 operation 4
#coroutine 2 operation 3
#coroutine 2 operation 4

我可以在操作 3 中使用阻塞方法,但这会阻塞整个服务器,我希望主循环不会执行某些协程,直到我告诉它们释放。

4

1 回答 1

0

在我仔细考虑之后,这很愚蠢。

这在单线程程序实践中真的非常简单和基本

global flag
while flag:
    do some asynchronous empty callback
flag = True

#operation 3
#use asynchronous method , but i'll use the result do 
#some process then update in operation 4
#switch to other coroutine

#operation 4
#use asynchronous method ,doesn't matter
#switch to other coroutine

flag = False

完毕。

于 2012-08-31T05:48:01.713 回答