Python Simpy类型错误

2024-04-29 03:51:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我在网上搜索了许多与类型错误相关的答案,并多次扫描我的代码,但我似乎看不到我遗漏的第三个参数是什么。我使用的是python2.7和simpy3

我的代码如下:

    import simpy
    import random

    RANDOM_SEED = 42
    NUM_SERVERS = 1
    MTBF = 10
    MTTR = 5
    TOTAL_ENGINES = 6
    TOTAL_SPARES = 3
    TOTAL_IN_USE = TOTAL_ENGINES - TOTAL_SPARES
    SIM_TIME = 100

    class Working(object):

        def __init__ (self, env, num, repair_facility, spares_inventory, downtime):
            self.env = env
            self.repair_facility = repair_facility
            self.spares_inventory = spares_inventory
            self.downtime = downtime
            self.name = 'Engine %d' % (num + 1)
            print('%s at %.2f' % (self.name, self.env.now))
            self.env.process(self.run())

        def run(self):
            yield self.env.timeout(random.expovariate(1.0 / MTBF))
            print('%s at %.2f' % (self.name, self.env.now))

            downtime_start = self.env.now
            spare = yield self.spares_inventory.get()
            self.downtime.append(self.env.now - downtime_start)

            print('%s at %.2f' % (spare.name, self.env.now))
            print('%d' % len(spares_inventory.items))

            with self.repair_facility.request() as req:
                yield req
                print('%s begins repair at %.2f' % (self.name, self.env.now))

                yield self.env.timeout(random.expovariate(1.0 / MTTR))

                yield self.spares_inventory.put(self)
                print('%s at %.2f' % (self.name, self.env.now))

            print('%d' % len(spares_inventory.items)) 

    def main():
        env = simpy.Environment()
        repair_facility = simpy.Resource(env, capacity = NUM_SERVERS)
        spares_inventory = simpy.Container(env, capacity = TOTAL_ENGINES, init = TOTAL_SPARES)
        downtime = []
        working = [Working(env, i, repair_facility, spares_inventory, downtime) for i in range(TOTAL_IN_USE)]

        env.run(SIM_TIME)   

    if __name__ == '__main__':
        main()

这是我一直得到的错误:

回溯(最近一次呼叫):

^{pr2}$

如有任何帮助,我们将不胜感激,谢谢


Tags: nameselfenvrandomnowattotaldowntime
1条回答
网友
1楼 · 发布于 2024-04-29 03:51:33

你在回溯中忘了一些额外的信息;在引用的回溯上,有几行是这样写的:

Traceback (most recent call last):
  File "/data/evertr/sw/lib/python2.7/site-packages/simpy/events.py", line 312, in _resume
    event = self._generator.send(event._value)
  File "simptest.py", line 31, in run
    spare = yield self.spares_inventory.get()
TypeError: __init__() takes exactly 3 arguments (2 given)

The above exception was the direct cause of the following exception:

然后是你的回溯。在

这样,您就可以看到self.spares_inventory.get()调用才是真正的罪魁祸首。令人厌烦的是,这个方法实际上是一个隐藏的类实例化(我注意到在simpy中有很多复杂的事情发生在幕后),这就是为什么您看到__init__()警告。在

基本上,您需要为amount提供一个self.spares_inventory.get()(无论好坏,都没有方便的缺省值1)。在

所以把它改成

^{pr2}$

可能会解决你的问题。在

(不过,在那之后你会遇到其他错误;你会发现的。这些新错误遵循相同的结构:一个回溯,然后是The above exception was the direct cause of the following exception行,然后是另一个(不太相关的)回溯)。在

相关问题 更多 >