I am using SimPy, and I try to simulate a network.
This is my main module:
from SimPy.Simulation import *
import node0
import message0
import network0
reload (message0)
reload (node0)
reload(network0)
initialize()
topology=network0.Network()
activate(topology, topology.operate())
node1=node0.Node(1)
node1.interface.send(destination='node1')
simulate(until=25)
I want an object of class message, which is activated by an object of class node, to interrrupt
class Message(Process):
def arrive(self, destination, myEvent=delay):
self.destination=destination
self.interrupt(topology)
an object of class Network (topology)
.
But I'm getting an error:
NameError: global name 'topology' is not defined
And I don't know how to make an object global. And if I type topology in python shell then it shows me object topology, so why can't message see it?
I'm pretty sure the issue is that your Message
class is defined in a different module than where your topology
variable is. So called "global" variables in Python are not really global (in the sense that there's just one global namespace), but just at the top of a specific module's namespace. So the global variable topology
in your main module's namespace is not accessible as a global variable from a different module.
My suggestion for working around this by passing the topology value to the Message
as a parameter to the __init__
method. If the message is being created by something other than your own code (e.g. by your Node
class), you might need to pass it around a bit more, so that it will be available when needed.
If that is not possible, you might be able to put the topology value in the namespace of a module that can be imported by your Message
code. This can get messy though, as circular imports can break things if you're not careful.