top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Benefits of asyncio with Python3?

0 votes
517 views

I have read that asyncio is a great addition to Python 3. I have looked around and saw the related PEP which is quite big BTW but couldn't find a simple explanation for why this is such a great addition. Any simple example where it can be used?

It can be used to have a queue of tasks? Like threads? Maybe light weight threads? Those were my thoughts but the library reference clearly stated that this is single-threaded. So there should be some waiting time in between the tasks. Then what is good?

These are just jumbled thoughts that came into my mind while trying to make sense of usefulness of asyncio. Anyone can give a better idea?

posted Jun 2, 2014 by Deepak Dasgupta

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

You're right, neither the PEP nor the docs to much to motivate the module's existence. I suggest that you look at the following link
http://en.wikipedia.org/wiki/Asynchronous_I/O

The asynchronous model lets you initiate a task (typically an I/O task) that would normally block, and then go on to do other things (like initiating more tasks) while waiting on that task, without having to resort to multiple threads or processes (which have the disadvantages of consuming more system resources as well as introducing the risk of race conditions and deadlocks).

It does this by using callbacks; when a task is complete, a callback is called that handles its completion. Often in asynchronous code you end up with large networks of callbacks that can be confusing to follow and debug because nothing ever gets called directly. One of the significant features of the asyncio module is that it allows asynchronous programming using co-routines, where the callbacks are abstracted away and essentially have the effect of resuming the co-routine when the task completes. Thus you end up writing code that looks a lot like threaded, sequential code with none of the pitfalls.

answer Jun 2, 2014 by anonymous
Similar Questions
+1 vote

I seem to stumble upon a situation where "!=" operator misbehaves in python2.x. Not sure if it's my misunderstanding or a bug in python implementation. Here's a demo code to reproduce the behavior -

From __future__ import unicode_literals, print_function

class DemoClass(object):
   def __init__(self, val):
   self.val = val

   def __eq__(self, other):
   return self.val == other.val

  x = DemoClass('a')
  y = DemoClass('a')

  print("x == y: {0}".format(x == y))
  print("x != y: {0}".format(x != y))
  print("not x == y: {0}".format(not x == y))

In python3, the output is as expected:

x == y: True
x != y: False
not x == y: False

In python2.7.3, the output is:

x == y: True
x != y: True
not x == y: False

which is not correct!!

+2 votes

I wanna simulate C style integer division in Python3.

So far what I've got is:

# a, b = 3, 4

import math
result = float(a) / b
if result > 0:
 result = math.floor(result)
else:
 result = math.ceil(result)

I found it's too laborious. Any quick way?

0 votes

I'm starting a new project from scratch so I think its finally a time to switch to the latest and greatest Python 3.4.

But I'm puzzled with MySQL support for Python 3. So far the only stable library I've found it pymysql.

All others are either abandoned work-in-progress projects or do not support Python 3:

  • mysqldb - Python 2.x only
  • mysql-ctypes - Python 2.x only
  • amysql - Python 2.x only
  • ultramysql - Python 2.x only
  • MySQL Connector/Python - new guy in block. Does anyone use it?
  • WebScaleSQL + MySQLdb1 - still in development, state unknown?

So what library do you use for MySQL access in Python 3? I'm specifically interested in async support (like in psycopg2 for PostgreSQL) since I'm planning to use Tornado.

0 votes

I need a command that will make threads created by "multiprocessing.Process()" wait for each other to complete. For instance, I want to do something like this:

job1 = multiprocessing.Process(CMD1())
job2 = multiprocessing.Process(CMD2())

jobs1.start(); jobs2.start()

PY_FUNC()

The command "PY_FUNC()" depends on the end result of the actions of CMD1() and CMD2(). I need some kind of wait command for the two threads that will not let the script continue until job1 and job2 are complete. Is this possible in Python3?

0 votes

I am trying to create one SSH tunnel, so that i can connect from a python script running on my pc, to a remote MySQL database running on my Host and id like to stick with Python 3.3 .

I contacted my host and he informed me that this is the only way.

I tried pycrypto + paramiko but from what i have noticed, paramiko is not Python 3.3 ready. Any thoughts?

...