Twisted
Aquest article tracta sobre el programari. Si cerqueu la pel·lícula, vegeu «Tomb inesperat». |
Tipus | Entorn de treball per a aplicacions web | ||||||
---|---|---|---|---|---|---|---|
Versió inicial | 22 d'octubre del 2002 | ||||||
Versió estable | 17.9.0 / 23 de setembre del 2017 | ||||||
Llicència | MIT | ||||||
Part de | Software Freedom Conservancy | ||||||
Característiques tècniques | |||||||
Sistema operatiu | Multiplataforma | ||||||
Escrit en | PHP | ||||||
Gestor de paquets | pip (en) | ||||||
Equip | |||||||
Creador/s | Glyph Lefkowitz | ||||||
Desenvolupador(s) | En comunitat | ||||||
Fonts de codi
| |||||||
Més informació | |||||||
Lloc web | twistedmatrix.com | ||||||
Seguiment d'errors | Seguiment d'errors | ||||||
Guia d'usuari | Guia d'usuari | ||||||
| |||||||
Twisted, en ciències de la computació, és un entorn de programació orientada a events en xarxa. Programació de successos implica que els usuaris escriuen petits callbacks que són cridats per l'entorn. Twisted està escrit en llenguatge Python.[1][2][3][4]
Arquitectura
- L'entorn es basa en successos associats a rutines manegadores de successos que s'executen quan s'activa l'event corresponent. Amb successos s'aconsegueix que els processos no quedin bloquejats, contràriament al que passa amb l'execució sense successos .[5]
- Totes les biblioteques de rutines callback han de ser sense bloqueig.
- Avantatges : millor aprofitament del temps del processador.
- Inconvenients : més difícil de codificar i mantenir.
- Suporta gran nombre de protocols : HTTP, XMPP, NNTP, IMAP, SSH, IRC, FTP, i altres.
Exemple
Exemple simple de client TCP :
# Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ An example client. Run simpleserv.py first before running this. """ from __future__ import print_function from twisted.internet import reactor, protocol # a client protocol class EchoClient(protocol.Protocol): """Once connected, send a message, then print the result.""" def connectionMade(self): self.transport.write(b"hello, world!") def dataReceived(self, data): "As soon as any data is received, write it back." print("Server said:", data) self.transport.loseConnection() def connectionLost(self, reason): print("connection lost") class EchoFactory(protocol.ClientFactory): protocol = EchoClient def clientConnectionFailed(self, connector, reason): print("Connection failed - goodbye!") reactor.stop() def clientConnectionLost(self, connector, reason): print("Connection lost - goodbye!") reactor.stop() # this connects the protocol to a server running on port 8000 def main(): f = EchoFactory() reactor.connectTCP("localhost", 8000, f) reactor.run() # this only runs if the module was *not* imported if __name__ == '__main__': main()
Referències
- ↑ jcalderone. «Twisted Web in 60 seconds: serve static content from a directory» (en anglès). http://jcalderone.livejournal.com,+16-09-2009.+[Consulta: 23 novembre 2017].
- ↑ Mcgreggor, Duncan. «Async Batching with Twisted: A Walkthrough» (en anglès). http://oubiwann.blogspot.com.es,+20-06-2008.+[Consulta: 23 novembre 2017].
- ↑ «Welcome to the Twisted documentation! — Twisted 17.9.0 documentation» (en anglès). http://twistedmatrix.com/.+[Consulta: 23 novembre 2017].
- ↑ «An introduction to Twisted» (en anglès). https://www.slideshare.net.+[Consulta: 23 novembre 2017].
- ↑ «Python Twisted» (en anglès). https://www.slideshare.net.+[Consulta: 23 novembre 2017].