Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding python twisted asynchronicity in terms of operating system

I'm new to the twisted library and I'm trying to understand how it is done that operations in python/twisted are performed asynchronously. So far I thought that only GUI-alike (Qt or javascript) platforms use event-driven architecture extensively.

facts:

  • Twisted programs are run in one thread = no multithreading
  • reactor and deferred patterns are used: callbacks/errbacks are declared and the execution of everything is controlled by reactor main loop
  • a single CPU can never do anything truly parallelly, because it shares its resources between processes, etc. By parallel code execution I mean that the programming platform (python, javascript, whatever) executes more than one sequence of operations (which can be done, for example, using multithreading)

question 1

Python could be seen as a high-level wrapper for the operating system. What are the OS functions (or C functions) that provide asynchronous operation handling? Are there any?

question 2

Q1 leads me to an idea, that twisted's asynchronicity is not a true asynchronicity, like we have in Javascript. In JavaScript, for example, if we provide 3 different buttons, attach callback functions to them and we click all three buttons - then the 3 callbacks will be executed parallelly. Truly parallelly.

In Twisted - as far as I understand - it's not true asynchronicity - it's, let's say, approximated asynchronicity instead, since no operations would be performed parallelly (in terms of code, as I mentioned in fact3). In Twisted the first n line of code (defining protocols, factories, connections, etc.) are the declarations of what is going to happen when entire system starts. Nothing runs so far. Real execution starts then the reactor.run() is fired. I understand that the reactor runtime is based on a single while True loop which iterates through events. The reactor checks any awaiting tasks to do, processes them, send their result back to the queue (either to callbacks or errbacks). In the next loop execution they'll be processed one step further. So the deferred execution is linear in fact (though, from outside it looks like it was executed parallelly). Is my interpretation correct?

I'd appreciate if someone could answer my questions and/or explain how asynchronicity works in twisted/python platform and how is it related to operating system. Thanks in advance for good explanations!

edit: links to articles explaining asynchronicity are very welcome!

like image 508
ducin Avatar asked Oct 06 '13 17:10

ducin


1 Answers

It's hard to talk about this without defining a lot of terms more precisely and taking issue with your facts, but here's my attempt:

Question 1:

Try man select, which is approximately how Twisted is implemented - it's a way to ask the operating system to monitor several things at once and let the application know when any one of them fires (block on multiple things).

Question 2:

Yeah, pretty much - but you're wrong about Javascript, it's just like Twisted.

like image 104
Thomas Avatar answered Sep 28 '22 09:09

Thomas