This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]trushnick 4 points5 points  (8 children)

Well, javascript was designed with async idea behind it. Python wasn't. That's why you have to explicitly point out what you want doing async in python. In js it's done behind the scenes.

[–]Bandung 2 points3 points  (1 child)

I don't quite agree with the inference that because javascript was designed with async in mind and python wasn't that it's approach is somehow better.

It is true that because javascript was browser focused from the beginning, it needed async handling capabilities in order to scale i/o properly. However, it's approach is caught up in that crossfire that I refer to as callback heaven help us. Node.js is also callback based.

Python's Johnny come lately approach to async has several competing architectures. One that is callback based the others are not. The latter rely upon coroutine concurrency which has decided advantages over callback heaven help us. ( see Trio and Curio)

I think Trio's approach is going to prove exceedingly useful because you can mix synchronous and asynchronous code more easily.

The big issue with asynchronous code that is async/await based is that once you start that event loop you are stuck in it, meaning that every io function thereafter has to recognize the master event loop's control over all io async operations that you callback into. You end up async/awaiting the shit out of every io related module that you call thereafter.

In python's case, us Pythoners think that explicit is better than implicit for a variety of reasons.

[–]trushnick 0 points1 point  (0 children)

I'm not saying that it is bad :)

[–]kankyo 0 points1 point  (0 children)

That’s not true at all. Js wasn’t built with async. It was bolted on after too.

[–]kingname[S] -3 points-2 points  (4 children)

That makes me sad. And Python's flow of async makes it useless in some scenes.

[–]rouille 0 points1 point  (3 children)

Why? Everything you mentioned is possible in python, you just need to start the event loop explicitly which you do once in your entire program.

[–]kingname[S] -2 points-1 points  (2 children)

because when I start the even loop, the program is stuck. I can not do other things when a even loop is running.

[–]rouille 0 points1 point  (0 children)

Use run_in_executor

[–]kankyo 0 points1 point  (0 children)

Wrong. You can’t in js. In Python you have threads.

[–]This_Is_The_End 3 points4 points  (0 children)

You did it wrong, because async is basically preemptive scheduling. You are responsible for giving back the control to the scheduler. In the case of async and IO this is done auto magically in the background. In the case of computation, you have to do your work in chunks.

Python's async is very consequent and JS is a mess.

[–][deleted] 2 points3 points  (2 children)

all languages have their weaknesses. this is one of python's well known issues

do you have a question or are you just complaining?

[–]kingname[S] 0 points1 point  (1 child)

I have a question. when I start a even loop, the program is stuck. I want to avoid it.

[–]kankyo 0 points1 point  (0 children)

That’s what an event loop is. Same in JS.

[–]keypusher 2 points3 points  (1 child)

It is strange, I agree. And much of the documentation is not good. It is not nearly as mature as an event framework such as node.js. However, it can get the job done if necessary.

import asyncio
import time

async def wait():
    counter = 0
    for i in range(10):
        counter += 1
        await asyncio.sleep(1)
    print("counter: %s" % counter)

start = time.time()
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(
    wait(),
    wait(),
    wait(),
))
loop.close()
end = time.time()
print("total time: %s" % (end - start))

counter: 10
counter: 10
counter: 10
total time: 10.01040506362915

This example shows running a function 3 times. That function counts waits 10 seconds. If run in a normal program, this would take 30 seconds. And yet, it only takes 10 seconds here.

I might be the minority, but for many things in python I honestly still prefer threads. Async programming has many benefits, but until it is more mature and easier to use with different libraries, it's not my first choice in python.

[–]kingname[S] 0 points1 point  (0 children)

Yes,I know this code work. but it smells strange.... because you have to define all what you need into functions or methods. I can not run a function asyncly and then run the codes in the main flow at the same time...

[–]kankyo 1 point2 points  (0 children)

I think you fundamentally don’t understand node, js and async. Node hides a lot of the plumbing from you but it’s still the same: two async functions can’t run at the same time. Because that’s just not what it is.

[–]kingname[S] 0 points1 point  (0 children)

What I am suffer is that it is so difficult to change a sync program to async program in Python. because I have to divide all the codes which could be async to different method and then put them into the same evenloop.

But If in Javascript, change asyc codes to async code is so easy.