我无法理解以下代码中的执行流程。无法解释错误以及输出

I am unable to understand the execution flow in the following code. Unable to explain the error as well as output

在下面的代码片段中,我试图理解 simpy 教程。我无法解释输出。我希望在打印其他语句之前一次性打印所有“\o/”。还有就是回调有问题

    import simpy
    class School:
    def __init__(self, env):
        self.env = env
        self.class_ends = env.event()
        self.pupil_procs = [env.process(self.pupil()) for i in range(3)]
        for x in self.pupil_procs:
            x.callbacks.append(self.my_callback(x))
            #print(x.callbacks)

        self.bell_proc = env.process(self.bell())

    def bell(self):
        for i in range(2):
            value=yield self.env.timeout(45,value="bell_rang!at"+str(env.now))
            self.class_ends.succeed(value='class_ends!')
            print("in bell proc")
            self.class_ends = self.env.event()
            print(value)


    def pupil(self):
        for i in range(2):
            print(' \o/',i, end=' ')
            classends=yield self.class_ends
            print('done!')
            print(classends)

    def my_callback(self,event):
        print('Called back from',event)


env = simpy.Environment()
school = School(env)
env.run()

输出结果如下:

Called back from <Process(pupil) object at 0x7fd7ddce97b8>
Called back from <Process(pupil) object at 0x7fd7ddce9be0>
Called back from <Process(pupil) object at 0x7fd7ddcad5f8>
 \o/ 0  \o/ 0  \o/ 0 in bell proc
bell_rang!at0
done!
class_ends!
 \o/ 1 done!
class_ends!
 \o/ 1 done!
class_ends!
 \o/ 1 in bell proc
bell_rang!at45
done!
class_ends!
done!
class_ends!
done!
class_ends!
Traceback (most recent call last):
  File "/home/deeplearning/PycharmProjects/python-scheduling/learnSimPy/learnmore.py", line 35, in <module>
    env.run()
  File "/usr/local/lib/python3.5/dist-packages/simpy/core.py", line 137, in run
    self.step()
  File "/usr/local/lib/python3.5/dist-packages/simpy/core.py", line 221, in step
    callback(event)
TypeError: 'NoneType' object is not callable

Process finished with exit code 1

您将 self.my_callback(x) 的结果(即 None)添加到回调列表中。

相反,您应该附加 functools.partial(self.my_callback, x).

我还建议您使用 yield from process 而不是将函数附加到 process 的回调。