Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add two integers together with Twisted?

I have two integers in my program; let's call them "a" and "b". I would like to add them together and get another integer as a result. These are regular Python int objects. I'm wondering; how do I add them together with Twisted? Is there a special performAsynchronousAddition function somewhere? Do I need a Deferred? What about the reactor? Is the reactor involved?

like image 228
Glyph Avatar asked Sep 06 '11 00:09

Glyph


2 Answers

OK, to be clear.

Twisted doesn't do anything about cpu bound tasks and for good reason. there's no way to make a compute bound job go any quicker by reordering subtasks; the only thing you could possibly do is add more compute resources; and even that wouldn't work out in python because of a subtlety of its implementation.

Twisted offers special semantics and event loop handling in case the program would become "stuck" waiting for something outside if its control; most normally a process running on another machine and communicating with your twisted process over a network connection. Since you would be waiting anyways, twisted gives you a mechanism to get more things done in the meantime. That is to say, twisted provides concurrency for I/O Bound tasks

tl;dr: twisted is for network code. Everything else is just normal python.

like image 199
SingleNegationElimination Avatar answered Nov 19 '22 00:11

SingleNegationElimination


How about this:

c = a + b

That should work, and it doesn't need to be done asynchronously (it's pretty fast).

like image 9
Keith Avatar answered Nov 19 '22 01:11

Keith