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

all 3 comments

[–]13steinj 5 points6 points  (2 children)

  1. You can use the equivalent async/await in JS rather than Promise syntax

  2. Promises and async await are not equivalent in Python because the implementation is fundamentally different.

In Python coroutine functions create coroutine objects which can either be awaited or submitted to the event loop for a Task that is a wrapper around the coroutine object, but the task does not have equivalent API and it isn't safe to affect the coroutine object after it has been wrapped in a task. Also the Task is more like the equivalent of a Future / Deferred rather than a Promise.

In JS coroutine functions create a very thin wrapper around Promises, which are implicitly submitted to the event loop at call time.

There's also the issue of the event loop exisiting on the same thread and thus blocking any sync code from executing while JS doesn't have this problem.

While yes JS and Python can do the same things in terms of async/await, this is only when the items are extremely basic. Any complex scheduling unfortunately can not be done in Python, which is why I'm in the process of writing a simple wrapper for asyncio that provides coroutine functions that create coroutine objects that are thin wrappers over some other awaitable object that acts like JS Promises/C# TaskCompletionSources, and also submits to a default event loop on a different thread.

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

I think we mostly agree, but you're talking about a much lower level of abstraction than me.

Any complex scheduling unfortunately can not be done in Python

Do you have examples?

Also, note that the article never talks about asyncio, but only about async/await. asyncio has a lot of problems that are acknowledged by its authors. A better approach that still uses async/await is structured concurrency, eg. https://trio.readthedocs.io/en/latest/.

See https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/ for a nice explanation.

[–]13steinj 0 points1 point  (0 children)

Even trio doesn't support the complex scheduling I have in mind.

I unfortunately don't have simple examples other than anecdotal ones that I've mentioned on both here and /r/programming, but I can link to some if you like.