[Twisted-Python] Best place to put application code
One thing that has been puzzling me is where is the best place to put application code. The case I am using is straightforward TCP server with client connections making simple requests and waiting for responses retrieved from database tables to be sent back. Reading the docs and looking at various examples provided in books and documentation has left me a bit confused. Is there a best practice for where application code should go - either the protcocol class or the factory class or somewhere else. Does it actually matter. Is there any downside to putting application code in the protocol or factory. What pitfalls are there for either approach. I see examples where application code appears in both classes, but the examples are very small so may not be indicative of what should be done. In the docs I see reference to most of the code will be written in the protocol class, but that seems to be referring to actually writing protocols not application code. It also says that when the protocol needs to call application code to make it a method call - not to mix protocol and application code. This could just mean creating some methods in the protocol class to handle the task. However, if the application code needs to run for 10-12 seconds looking up database tables and accumulating results and waiting on deferreds, should all this code reside in the protocol class or the factory. If I keep it in the protocol, then I already have my client connection to write back to. So that seems to be the place to keep the code. If I put the code in the factory, then I need to pass the client connection so it can write back to the client. Or is there another way of doing this I have missed. . The factory seems to be the place where other classes can be passed in and the protocol can call them via self.factory. That seems to imply that application code should be put into the factory, but I can't see any way of passing back information from deferred results to the call from protocol. It's ok if it was just a simple method call that returns a result, but if the code has to run a series of deferreds then it will be the called method that will have the result and it will need a means of writing this back to the client. I don't think I can signal the protocol to say I now have the result.Of course I could easily be mistaken. So please correct. Of course if I passs the client connection to the factory, then it can use this to write back. But that means passing around the client connection. Should I avoid doing that or is that not a problem. I hope I have explained myself clearly. I'm just looking for some guidance and pointers to what is best to do or what is best to avoid. Regards John Aherne ___ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Re: [Twisted-Python] ssh v1 support
Thanks Cary. Are there any available easy to use library other than twisted that supports ssh v1? Deniz On Fri, Dec 26, 2008 at 4:17 PM, Cary Hull wrote: > Not out of the box. Conch is ssh2 only. However, you can attain ssh1 > support by wrapping the binary (ssh) with a ProcessProtocol: > > http://twistedmatrix.com/projects/core/documentation/howto/process.html > > Before you start on your own implementation I would wait to see if > anyone has done this already and wouldn't mind sharing. :) > > -Cary > > On Fri, Dec 26, 2008 at 7:47 AM, Deniz Pecel wrote: > > Hi List > > Does twisted support ssh v1? > > thanks > > > > ___ > > Twisted-Python mailing list > > Twisted-Python@twistedmatrix.com > > http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python > > > > > > > > -- > 01100011 0111 01110010 0001 > > ___ > Twisted-Python mailing list > Twisted-Python@twistedmatrix.com > http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python > ___ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Re: [Twisted-Python] Best place to put application code
On Fri, 2 Jan 2009 10:14:40 +, John Aherne wrote: One thing that has been puzzling me is where is the best place to put application code. It depends. :) The case I am using is straightforward TCP server with client connections making simple requests and waiting for responses retrieved from database tables to be sent back. Reading the docs and looking at various examples provided in books and documentation has left me a bit confused. Is there a best practice for where application code should go - either the protcocol class or the factory class or somewhere else. Does it actually matter. Is there any downside to putting application code in the protocol or factory. What pitfalls are there for either approach. Best practice for a Protocol class is to include code which is necessary to interpret bytes which are received and turn them into a structured form which is easier to deal with; code which starts from some structured form and emits bytes to be sent should also be part of a Protocol class. It is common practice to have a class which includes just these things and then a subclass which adds application-specific logic based on top of this functionality. It is also common practice to connect a protocol which has only these things, no application-specific code, and then have application code elsewhere (in a free function, a method of a factory, another class's method, user input, etc) make calls onto it. Which of these approaches is most well suited to a particular application depends. For example, if the application code creates multiple connections with shared state adding the application logic to a Protocol subclass isn't a good approach. I see examples where application code appears in both classes, but the examples are very small so may not be indicative of what should be done. Generally they're so small that there's no advantage to any approach over any other, yes. In the docs I see reference to most of the code will be written in the protocol class, but that seems to be referring to actually writing protocols not application code. It also says that when the protocol needs to call application code to make it a method call - not to mix protocol and application code. This could just mean creating some methods in the protocol class to handle the task. Consider all of the code you write to be part of a library you're developing. If you implement a protocol, then you've just written part of a library which provides a slightly higher-level API for interacting with the network in some way. With that in hand, you can move on to some other part of your library which uses that higher-level API to accomplish something even higher-level, perhaps presenting yet another API to some other part of your application which is higher-level still. The motivation to not mix protocol and application code is just the motivation to have clear boundaries in your library to make as much of it reusable as possible. If you have a protocol implementation mixed together with application A, when you come along to write application B which needs to use the same protocol, you'll have to re-implement the protocol, or refactor your original implementation to move the application A code elsewhere (of course, there's nothing wrong with having to refactor your code - it's a common part of programming, and since it's very difficult to predict the future, it's often best *not* to try to anticipate your future requirements when writing code - just write what works and is easily testable, and when your future requirements come along, deal with them then; as you do this more and more, you'll probably get a sense of how to structure your code to minimize the effort required for refactoring, but aside from experience with this process, I don't know of any way to learn this skill). However, if the application code needs to run for 10-12 seconds looking up database tables and accumulating results and waiting on deferreds, should all this code reside in the protocol class or the factory. I wouldn't take the duration of the task into consideration when trying to decide where to put it. I'd consider reusability, testability, simplicity, and correctness. If I keep it in the protocol, then I already have my client connection to write back to. So that seems to be the place to keep the code. If I put the code in the factory, then I need to pass the client connection so it can write back to the client. Or is there another way of doing this I have missed. This isn't much different from the trade-off you have to consider when you decide to implement anything as two classes instead of one. Since you can no longer just use `self´ everywhere, you'll have to figure out how to get a reference to the other instance that you need sometimes. This shouldn't be difficult though - just invoke a method on one class with an instance of the other. . The factory seems to be the place where other classes can be passed in and
Re: [Twisted-Python] Best place to put application code
On Fri, Jan 2, 2009 at 5:14 AM, John Aherne wrote: > One thing that has been puzzling me is where is the best place to put > application code. business logic should go in the "Service" class. ___ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
[Twisted-Python] [Q] multi-process chat client and poller
Hi, I am in the process of evaluating a tool for the newtorking architecture I need. And wanted to ask the group questions, I could not easily get answered from reading docs and various posts: Basically my architecutre requires the following a) multi-CPU/multi-core scalability b) multi-machine horizontal scalability c) Event dispatching: having a single 'dispatcher' instance being able to read rows (events) from postgresql database and being able to dispatch it to available 'worker' processes d) chat server For c) and d) am looking for a framework such that it will allow me to develop the 'worker' processes such that they reside on mulitple machines, and a lot of the 'hard work' of registration, message passing, restarting, SNMP compatibilty . Twisted appears to already have message passing, chat server and many many other neat things that if I do not need now -- probably will need in the future. However, I cannot quite understand how the 'multi-process' part is supported. I have read this: http://www.python.org/workshops/2002-02/papers/09/index.htm (found it via Bruce Eckel's log) and it says that: "... and since forking Python processes has many disadvantages, like Python's reference counting not playing well with copy-on-write and problems with shared state, it was felt the best option was an event-driven framework. " I also looked at the docs for the http://twistedmatrix.com/projects/core/documentation/howto/process.html But I got confused there Basically I am looking to 'pre-spawn' a number of worker threads configured from command line (and each of the worker threads will precreate a database connection). So each multi-cpu machine will have one twisted Server, each server will pre-spawn N worker processes and each process will have its own database connection (and therefore each can do its own caching and transaction control (or use memchaced server) to share cache). I did not see however how to: a) make the Deferred mechanism to pass the event data received in the Asynch loop to one of the worker processes (there are appeared to be no 'inheritance' structure to where derive the worker processes from). b) I did not see how multiple twisted servers, each running on a separate multi-cpu machine can register together to be in one 'cluster' -- so that my dispatcher process that reads the events from the database can 'round-robin' the events to them. c) I did not see if twisted 'figures out' that given process runs on a local machine vs remote and optimises the IPC communication for local IPC. Because I did not see the above -- it lead me to believe that I am trying to ask Twisted have something it was not meant to do (My view of the architecture is somewhat similar to how would ACE/TAO ORB notification service would work -- as I am familiar with those). I kind of did not go into the multi-box architecutre of how the Twisted chart server is working, because I thought I need to understand the above first. So wanted to ask you guys if I am looking for the right documentation or may be there is a separate sub-project that is doing what I am looking for. thanks in advance, Vlad -- V S P torea...@fastmail.fm -- http://www.fastmail.fm - Or how I learned to stop worrying and love email again ___ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python