Alec
Alec

Reputation: 979

Reactor.run freezes loop

I have a Twisted socket that I am attempting to run on multiple ports. The following code has worked for me before, but that was around 1 month ago as I have not touched the code since, if I remember right. Now, after re-entering the code into my Twisted program, it doesn't work anymore.

class Socket(Protocol):
    table = Table()

    def connectionMade(self):
        #self.transport.write("""connected""")
        self.factory.clients.append(self)
        print "Clients are ", self.factory.clients

    def connectionLost(self, reason):
        self.factory.clients.remove(self)

    def dataReceived(self, data):
        #print "data is ", data
        a = data.split(':')
        if len(a) > 1:
            command = a[0]
            content = a[1]

            if command == "Number_of_Players":
                msg = table.numberOfPlayers


        print msg

        for c in self.factory.clients:
                c.message(msg)

    def message(self, message):
        self.transport.write(message)

NUM_TABLES = 10

factories = [ ]
for i in range(0, NUM_TABLES):
    print i
    factory = Factory()
    factory.protocol = Socket
    factory.clients = []
    factories.append(factory)
    reactor.listenTCP(1025+i, factory)
    #print "Blackjack server started"
    reactor.run()

It would usually print Blackjack server started however many times in the range I set, but now it doesn't. To test if it was looping over or not, I set out to print i, but it only printed 0. For some reason, the for loop only looped 1 time.

Any suggestions? Thanks!

Upvotes: 2

Views: 1314

Answers (3)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115520

Twisted programs usually have only one reactor running. Keep in mind that when you start (.run()) the reactor, execution passes inside the reactor loop (and the various events that you have defined in your code, like connectionMade(), connectionLost(), dataReceived(), etc are triggered when the respective actions take place). Whatever code is after the reactor.run() is executed only after reactor is stopped.

So, your code never passes the first iteration of the for loop.

Try moving the reactor.run() out of the loop:

NUM_TABLES = 10

factories = [ ]
for i in range(0, NUM_TABLES):
    print i
    factory = Factory()
    factory.protocol = Socket
    factory.clients = []
    factories.append(factory)
    reactor.listenTCP(1025+i, factory)

# print "Blackjack server started"

reactor.run()

# whatever code you put here, is executed only **after reactor has stopped**

Upvotes: 5

Levon
Levon

Reputation: 143037

Do you get any sort of error message? There's nothing wrong with your basic loop. I'd suggest commenting out all the lines below print i and then selectively add them back until the loop stops - that will tell you what is causing the problem (obviously the 2nd print is not a problem)

Perhaps the listenTCP() is hanging waiting for a connection?

Or possibly you have some sort of infinite loop in one of the functions you are calling. Only way to be certain w/o seeing the other code is to eliminate possible problems by commenting out suspect code and adding it back in.

Update: Based on your new information, the problem obviously is with reactor.run(). If the code is self-contained enough you could post it for examination.

Two potential problems in reactor.code(), an infinite loop, or the program is waiting for some sort of event/input.

The loop may be infinite, i.e., coded incorrectly so that it starts but never terminates. That would look as if the program is pausing/stopping (even though the CPU is cranking like crazy). Alternatively, the program could pause if it was waiting for some input/event.

Upvotes: 2

Jirka Hanika
Jirka Hanika

Reputation: 13529

An exception was thrown before you reached the debugging print.

Catch it by wrapping the shown block with try: ... except.

try:
       reactor.listenTCP(1025+i, factory)
except IOError as (errno, strerror):
    print "I/O error({0}): {1}".format(errno, strerror)
except:
    print "something else happened"

Upvotes: 1

Related Questions