我正在玩机器人模拟的简单操作,我开始写一个简单的乒乓球比赛,有两个球拍叫乒乓球。只有当我添加一个yield env.timeout(0)
让一个玩家为对方玩家提供机会才能抓住他的回合时,它才有效。如果我跳过这个noop(?),第一个球员一直抓住球。这是我的代码:
import simpy
ball_wait = 1
def racket(env, name, ball):
while True:
# Let the first user catch the ball
with ball.request() as req: # Create a waiting resource
yield req # Wait and get the ball
# The time it takes for the ball to arrive. This can
# be used to plan the strategy of how to hit the ball.
yield env.timeout(ball_wait)
print env.now, name
# "Sleep" to get the other user have his turn.
yield env.timeout(0)
env = simpy.Environment()
ball = simpy.Resource(env, capacity = 1)
env.process(racket(env, 'Ping', ball))
env.process(racket(env, 'Pong', ball))
env.run(until=10)
print 'Done!'
我的问题是为什么我需要env.timeout(0)
?我也想知道是否还有一些其他(更好的?)策略在两个进程之间交换控制权?我也玩process.interrupt()
,但在我看来,这似乎是一种矫枉过正。