515 karma · joined Dec 27, 2017 (8y ago) · 421 submissions
comment 6y ago · link · parent
I've been stung, time and again by asyncio.create_task() swallowing exceptions[1], and functions not telling callers about background tasks[2].
For others looking to NOT rewrite all their code using a 3rd party event loop, here's a simple reproduction of nursery's behaviour in raw asyncio -
Hopefully, this will help lift the curse of asyncio :)
[EDIT] A library is also available - https://github.com/Tygs/ayo (with amazing operator overload)
async def main():
async with Nursery() as ny:
ny.start_soon(a_1(), a_2(), ..., a_n())
ny.start_soon(b_1(), b_2(), ..., b_n())
.
.
.
ny.start_soon(x_1(), x_2(), ..., x_n())
class Nursery:
def __init__(self):
self.tasks = set()
def start_soon(self, *coros: typing.Coroutine):
for coro in coros:
self.tasks.add(asyncio.create_task(coro))
async def __aenter__(self):
return self
async def __aexit__(self, *args):
try:
while self.tasks:
tasks = self.tasks
self.tasks = set()
done, pending = await asyncio.wait(
tasks, return_when=asyncio.FIRST_COMPLETED
)
try:
for task in done:
await task
finally:
self.tasks |= pending
finally:
for task in self.tasks:
task.cancel()
[1] https://stackoverflow.com/questions/60287285/starlette-async...comment 6y ago · link · parent
[1] https://docs.python.org/3.8/library/concurrent.futures.html#...
comment 6y ago · link · parent
I believe concurrent.futures is meant as a way for asyncio to spawn threads or processes -- which lets you essentially call sync code from async and not have it block the event loop.
From the docs[1] -
The concurrent.futures module provides a high-level interface for asynchronously executing callables.
The asynchronous execution can be performed with threads, using ThreadPoolExecutor, or separate processes, using ProcessPoolExecutor. Both implement the same interface, which is defined by the abstract Executor class.
[1] https://docs.python.org/3.8/library/concurrent.futures.html
comment 6y ago · link · parent
Here's a rough sketch of how this can be achieved for a language that already has event loops -
Transpile the following -
void main() {
Future<int> xFuture = async doSomething()
int x = doSomething()
assert(await xFuture == x);
}
Into - Future<void> main() async {
Future<int> xFuture = (() async => await doSomething())();
int x = await doSomething();
assert(await xFuture == x);
}
Now people can keep using a pre-existing `doSomething()` (that's either async or sync) and its compatible with our new caller based async statement.BTW that transpiler output is runnable dart code actually works.
I know this has tons of edge cases that this simple example doesn't capture, but it's definitely "possible".
comment 6y ago · link · parent
I mean for languages that have event loops at their core, why isn't every function `async` by default? Let the caller decide how it wants to use the function.
`async` functions don't wait for a result of a `Future` unless `await` is used.
Instead of putting all that effort into putting `await` everywhere, why not invert that logic and make them `await` by default, and instead have the async-await syntax used by callers?
Like if one to were to write a transpiler to do this in dart, it might compile the following -
void main() {
Future<int> xFuture = async doSomething()
int x = doSomething()
assert(await xFuture == x);
}
int doSomething {
...
}
into - Future<void> main() async {
Future<int> xFuture = doSomething()
int x = await doSomething()
assert(await xFuture == x);
}
Future<int> doSomething async {
...
}
This might be stupid, but my solution to the "what color is your function problem" is to make every function async.I looks an awful lot like what go does with its `go` syntax, but because you have an event loop with Future and Async Streams, you don't need to worry about dealing with CSP.
comment 6y ago · link · parent
comment 6y ago · link · parent
Like here's David's "Build Your Own Async" [1], which I prefer over Philip's (still extremely good) "What the heck is the event loop anyway?" [2].
It's one thing to tell how something works, but to successfully show what the hell its actually doing under the covers, just conveys much more information.
[1] https://www.youtube.com/watch?v=Y4Gt3Xjd7G8 [2] https://www.youtube.com/watch?v=8aGhZQkoFbQ
comment 6y ago · link · parent
A similar snippet[1] exists for go, and it doesn't do anything particularly special either.
I don't know, maybe David beazley has altered my sense of what "from scratch" means.
[1] https://gist.github.com/lizrice/a5ef4d175fd0cd3491c7e8d71682...
comment 6y ago · link · parent
While you're technically programming using an imperative language, they've somehow managed to make the syntax close enough to a declarative one that it isn't just a soup of bare object surgery (like tkinter or java swing).
They also, really went the extra mile to NOT have to do tree diffing[1], by making Widgets immutable, while still having a "detached" State associated with them.
And this is eerily close to the JSON-is-HTML syntax the author is proposing. A replication of flutter's strategy in pure JS is defininitely worth exploring.
[1] https://flutter.dev/docs/resources/inside-flutter#linear-rec...
comment 6y ago · link · parent
Last time I used it, KDE had _proper_ fractional scaling and rendered fonts at full resolution compared to mac, which by default runs at lower than native resolution of my retina MacBook pro.
138.
Ask HN: How to sync server and app deploys?
1 points · by devxpy · 6y ago · 2 comments
149.
Jancy – scripting language with C ABI/source compatibility(github.com ↗)
1 points · by devxpy · 7y ago · discuss
comment 7y ago · link · parent
Perhaps it's possible to just use PL/Python[2] to write a custom stored prodecure?
[1] https://docs.hasura.io/1.0/graphql/manual/schema/default-val...