Search code examples
pythonpython-2.7typeerrorsimpy

Type Error - Python Simpy


I have searched online for many answers related to TypeError and scanned through my code multiple times but I can't seem to see what is the 3rd argument I'm missing. I am using Python 2.7 with Simpy 3

My code is as follows:

    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()

This is the error I keep getting:

Traceback (most recent call last):

      File "", line 61, in <module>
        main()
      File "", line 55, in main
        env.run(SIM_TIME)
      File "", line 120, in run
        self.step()
      File "", line 213, in step
        raise event._value
      TypeError: __init__() takes exactly 3 arguments (2 given)

Any help at all is much appreciated, Thanks a lot in advance


Solution

  • You forgot some extra info in your traceback; above your quoted traceback, there's a a few lines that say:

    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:
    

    followed by your traceback.

    With that, you can see that the self.spares_inventory.get() call is the real culprit. Annoyingly enough, this method is actually a hidden class instantiation (lots of tricky stuff happening behind the scenes in simpy, I've noticed), and that's why you see the __init__() warning.

    Basically, you need to supply an amount to self.spares_inventory.get() (there's, for better or worse, no convenient default of 1).

    So changing that to

    spare = yield self.spares_inventory.get(1)
    

    may solve your problem.

    (You'll run into other errors after that though; you'll find out. Those new errors follow the same structure: a traceback, followed by the line The above exception was the direct cause of the following exception, followed by another (less relevant) traceback).