gigimon
gigimon

Reputation: 1521

twisted and more clients in script

Hello
I'm new in twisted, but have some questions after read manual:
1. How to use different protocols with different reactor in one program? (for example, txNetTools have own reactor and internal IRC support in twisted have own reactor from twisted.internet)
2. How to start more one client in one time? (many client with ping to other remote host) http://bazaar.launchpad.net/~oubiwann/txnet/trunk/view/head:/sandbox/ping.py
3. How to put data from one protocol to other (in one program)? I'm want use data from database in protocol. (for example, every 5 minutes get hosts from database and create ping clients)
My task is simple, create a more different protocol client to many count of servers.

Upvotes: 1

Views: 364

Answers (2)

Jean-Paul Calderone
Jean-Paul Calderone

Reputation: 48335

How to use different protocols with different reactor in one program?

You don't. There is only one reactor per process, and it can handle as many connections as you want it to. The vast majority of libraries don't provide a reactor, and the reactor provided by txNetTools is optional. The only thing it provides is this method:

def listenICMP(self, port, protocol, interface="", maxPacketSize=8192):
    p = icmp.Port(port, protocol, interface, maxPacketSize, self)
    p.startListening()
    return p

If you want to use another reactor, then you can just instantiate an icmp.Port yourself.

How to start more one client in one time?

The same way you start one, but repeated. For example, here's ten concurrent pingers (incorporating the answer to the first question):

for i in range(10):
    p = icmp.Port(0, Pinger(), reactor=reactor)
    p.startListening()
reactor.run()

chameco gives a fine answer to the last question.

Upvotes: 1

Samuel Breese
Samuel Breese

Reputation: 714

Well, for the third question at least, are you talking about using protocols of different classes or multiple protocol instances of the same class? Protocol instances can communicate between each other by having the factory that creates them store their data, like the following:

class p(Protocol):
    factory = None
    ...
class f(Factory):
    protocol = p
    data = None
    def buildProtocol(self, addr):
        returnValue = p()
        returnValue.factory = self
        return returnValue

From there you can save data to self.factory.data from within a protocol instance, and any other protocol instance can access it. I hope that answered your question.

Upvotes: 1

Related Questions