Skip to content

Instantly share code, notes, and snippets.

@Whateverable
Created April 19, 2019 21:48
Show Gist options
  • Save Whateverable/6050c75ac308e3b0fddffc09a20077cb to your computer and use it in GitHub Desktop.
Save Whateverable/6050c75ac308e3b0fddffc09a20077cb to your computer and use it in GitHub Desktop.
quotable6
Promise .*? but

uses Channel much less than Promise and Supply, but that's probably a reflection of the problems he works on
mine is something like await Promise.anyof(Promise.in(10),start {...await $c...}) but that inner await seems to allow the outer to unblock...or someting
m: my $S = Supplier.new; my $s = $S.Supply; my $p = Promise.new; start react { whenever $s { "foo".say }; $p.keep; }; await $p; $S.emit("foo") for ^4; sleep 0.2 # Seems to work... but I'm unsure if there are tests/docs that promise it always will.
(I mean, Promise.in is fine, but the .then is less so)
hello. I am trying to get the Pelr6 advent Day 15 – A Simple Web Spider With Promises to run. It failed initially till i changed urls_seen to url_seen at line 35. but whenever i run it with a domain, the results are blank.
I'm pretty familiar with Promises in ES6, but I'm not sure what the differences are. I suspect that there are some.
hi, does someone know if there is a library function to get the first Promise ~~ Kept but try all of them in case they all break? I have something, but I think it is a bit elaborate https://pastebin.com/DUV1X7Vf
Timer::Breakable isn't actually cancelling anything. It's just wrapping the block passed to Promise.in() with a check to see if the code still needs run. So if you stop it the internal promise still runs but does nothing.... this is probably a really bad idea.
lizmat: I was thinking... the way that is sad to make a timeout, is to use Promise.anyof($promise-that-should-timeout, Promise.in($timeout))... but it doesn't really times out... the promise continue running...
the Promise.in(3) internally most likely *does* fire, but the code block is not executed because it's no longer inside the react block
somehow I read the block as being part of the Promise, but it isn't
rakudo-moar 2fb8c7: OUTPUT: «Tried to get the result of a broken Promise? in block <unit> at <tmp> line 1??Original exception:? Too few positionals passed; expected 2 arguments but got 1? in block <unit> at <tmp> line 1??»
A Proc::Async has one exit code (Promise) but may write to stdout/stderr many times (Supply)
rakudo-moar 0095cd: OUTPUT: «(&infix:<+|> WhateverCode SIGSEGV Slip &callframe Pair &keys SIGHUP utf8 &RETURN-LIST &infix:<∖> CurrentThreadScheduler PromiseStatus StringyEnumeration &infix:<ne> Distribution &infix:«<=» &slip &splice &trait_mod:<handles> &callsame &GATHER Backtra…»
rakudo-moar 9e8ecb: OUTPUT: «((Slip) (PromiseStatus) (StringyEnumeration) (Distribution) (int16) Nil (Setty) (FileChangeEvent) (UInt64) (Rational) (RatStr) (uint64) (Numeric) (Instant) (byte) (Bool) (Associative) (Iterator) (uint32) (ProtocolFamily) (Rat) (Enumeration) (Blob[uint16]…»
rakudo-moar 5592db: OUTPUT«((WhateverCode) (Slip) (Pair) (utf8) (CurrentThreadScheduler) (PromiseStatus) (StringyEnumeration) (Distribution) (Backtrace) (NFKC) (Duration) Nil (Any) (WrapDispatcher) (IO) (FileChangeEvent) (UInt64) (Order) (RatStr) (uint64) (Numeric) (Cursor) (Proxy) …»
rakudo-moar 5592db: OUTPUT«Doing Signal␤Doing WhateverCode␤Doing Sub␤Doing Pair␤Doing Signal␤Doing Sub␤Doing Sub␤Doing utf8␤Doing Sub+{<anon|71898608>}+{Precedence}␤Doing CurrentThreadScheduler␤Doing PromiseStatus␤Doing StringyEnumeration␤Doing Distribution…»
rakudo-moar d9c735: OUTPUT«(SIGSEGV WhateverCode Slip &callframe Pair SIGHUP &flat &RETURN-LIST utf8 &infix:<∖> CurrentThreadScheduler PromiseStatus StringyEnumeration Distribution &splice &postcircumfix:<{ }> &callsame &GATHER Backtrace PF_INET6 &sinh &infix:<lt> &print &asech NF…»
rakudo-moar d9c735: OUTPUT«WhateverCode␤Slip␤Pair␤utf8␤CurrentThreadScheduler␤PromiseStatus␤StringyEnumeration␤Distribution␤Backtrace␤NFKC␤Duration␤Nil␤Any␤WrapDispatcher␤IO␤FileChangeEvent␤UInt64␤Order␤RatStr␤uint64␤Numeric␤Cursor␤Proxy…»
about failures from #perl6-dev: https://gist.github.com/anonymous/bfa6d799ccc890be63eafa5ea75009c2 + backlog copy; it's surprising Promise do not throw just like Failure. but nice :)
Plus, in Haskell you get things like Software Transactional Memory, which, together with constructs that work a bit like Promises and the like, give you a most-robust way to do something like variables, but safely, so that they _will not_ go wrong.
ZzZombo: it's a check for whether a Promise was Kept or Broken (but can be a simple bool)
Hello all, is there an object like Channel that may fail on send, what I mean: is there an object like Channel where send is a Promise? A bit like Socket::Async but without the Socket implemented
rakudo-moar 5f9103: OUTPUT«Invocant requires an instance of type Promise, but a type object was passed. Did you forget a .new?␤ in block <unit> at <tmp> line 1␤␤»
m: say Promise.^attributes
I was hoping I could do the equivalent of (&f1, &f2).map(&Promise.start), but it seems like a non-trivial thing to write
AlexDaniel, I'm not well read on Promise, but the docs say that start() returns a Promise that is kept by a Proc object.
rakudo-moar 02fdcf: OUTPUT«(SIGSEGV WhateverCode Slip &callframe Pair SIGHUP &flat &RETURN-LIST utf8 &infix:<∖> CurrentThreadScheduler PromiseStatus StringyEnumeration Distribution &splice &postcircumfix:<{ }> &callsame &GATHER Backtrace PF_INET6 &sinh &infix:<lt> &print &asech NF…»
The second half of it is tossing `await` and instead shooting off a new Promise inside of which the callable calls will continue. Basically the equivalent of wrapping the whole thing in `start {}` but doing so only if at least one Callable returns a Promise.
This is my first attempt to use Promises. My search for a longest substring can work independently over the various string lengths, so I thought I might try to parallelize this. It's only going up to 20 instead of the full length because it's running wicked slow and I wanted to see if it would indeed process things not in order. It does, but am I doing something wrong to make it so slow?
rakudo-moar 04f0b2: OUTPUT«(SIGSEGV WhateverCode Slip &callframe Pair SIGHUP &flat &RETURN-LIST utf8 &infix:<∖> CurrentThreadScheduler PromiseStatus StringyEnumeration Distribution &splice &postcircumfix:<{ }> &callsame &GATHER Backtrace PF_INET6 &sinh &infix:<lt> &print &asech NF…»
Hey all. jberger is trying to calculate pi using monte carlo method. I tried to write it with .race, but it was slower than single-thread way. Now, I rewrote it with Promises, but it *still* doesn't offer any speed improvement on my 4-core box. Any idea why? Is my code wrong: https://gist.github.com/zoffixznet/ae7bcf0efc99eba8eaadbd917238ff1a
jnthn: Is is by design that when an exception occurs inside a `Promise.start/then` block, the backtrace does not show the line-number and routine-name where it occurred, but only the line where the Promise ended up being awaited?
rakudo-moar 1d1256: OUTPUT«(SIGSEGV WhateverCode Slip &callframe Pair SIGHUP &flat &RETURN-LIST utf8 &infix:<∖> CurrentThreadScheduler PromiseStatus StringyEnumeration Distribution &splice &postcircumfix:<{ }> &callsame &GATHER Backtrace PF_INET6 &sinh &infix:<lt> &print &asech NF…»
i.e. osx has some problem with `await Promise.allof(@promised)`, but `for @promised { await $_; }` works, with the difference being the first one doesnt seem to sink all the promises so they never get out of Planned state
azawawi: "phantomjs returned Proc::Async promise: Promise.new(scheduler => ThreadPoolScheduler.new(initial_threads => 0, max_threads => 16, uncaught_handler => Callable), status => PromiseStatus::Planned)" but it's using the wrong port
rakudo-moar 775271: OUTPUT«(SIGSEGV WhateverCode Slip &callframe Pair SIGHUP &flat PromiseStatus CurrentThreadScheduler &infix:<∖> &RETURN-LIST utf8 StringyEnumeration &splice Distribution PF_INET6 Backtrace &GATHER &callsame &postcircumfix:<{ }> &print &infix:<lt> &sinh &GENERATE…»
rakudo-moar 42a583: OUTPUT«(SIGSEGV WhateverCode &FILETEST-X Slip &callframe Pair SIGHUP &flat PromiseStatus CurrentThreadScheduler &RETURN-LIST utf8 &MAKE-DIR-LIST &infix:<∖> StringyEnumeration Distribution &splice &postcircumfix:<{ }> &callsame &GATHER Backtrace PF_INET6 &sinh &…»
llfourn: I can't Promise, bad pun, right now as I need to wrap my head around this and use it. but I will eventually (after my move to another town and the build of the ground of my summer house, not too long)
a quick style/taste question if I have a method that will return a Promise but it can detect that it just won't work up front is it best to create a new Promise and break it with an exception or do the Promise.start anyway and throw the exception in there?
await Promise.allof($p1, $p2) # should do it, but seems allof throws if one of the promises fails
rakudo-moar e6f360: OUTPUT«SIGSEGV WhateverCode bit &FILETEST-X &callframe Pair SIGHUP &flat utf8 &TRANSPOSE &MAKE-DIR-LIST &infix:<∖> CurrentThreadScheduler PromiseStatus StringyEnumeration Distribution &splice &postcircumfix:<{ }> &callsame &GATHER Backtrace &sinh &first-index &…»
rakudo-moar a6bb0b: OUTPUT«(SIGSEGV WhateverCode bit &FILETEST-X Slip &callframe Pair SIGHUP &flat PromiseStatus &RETURN-LIST utf8 &TRANSPOSE &MAKE-DIR-LIST &infix:<∖> CurrentThreadScheduler StringyEnumeration Distribution &splice &postcircumfix:<{ }> &callsame &GATHER Backtrace &…»
Suppose I want to make a Proxy that backs a Promise. I can implement STORE to replace what it will return with, but I can't really implement FETCH (unless I either die, or freeze, or something). But, it would still make sense for me to do something like += 1 on it, but I can't do that if I can only define STORE/FETCH. So maybe MODITY/FETCH makes more sense, and storing is just a special case of MODIFYing
rakudo-moar a6582d: OUTPUT«SIGSEGV WhateverCode bit &FILETEST-X &callframe Pair SIGHUP &flat utf8 &TRANSPOSE &MAKE-DIR-LIST &infix:<∖> CurrentThreadScheduler PromiseStatus StringyEnumeration Distribution &splice &postcircumfix:<{ }> &callsame &GATHER Backtrace &sinh &infix:<lt> &f…»
not sure if it's exactly a Promise, or just a block that'll get called the first time you access it, but yeah
any really - you can get plain cue({}) and cue({}, in => 1) from Promise but every and times not
i'm not sure what kind of async library one would look for, with Promises, Supplies and start/await in core, but that might just be because i'm not particularly knowledgable
all accesses *should* go through the .vow method in Promise, but they don't have to
I'm reading up on Promises, and I wonder: How do I break a Promise? I tried "$promise.vow.break;" but I'm getting: Access denied to keep/break this Promise; already vowed
moritz: When I run "my $breakable = Promise.new; my $p = Promise.anyof(start { sleep 5; 42 }, $breakable);", I'm getting: Too many positionals passed; expected 1 argument but got 2
yer welcome :) wrt alarm(), I'm pretty sure we don't have it in lieu of higher-level timing and async stuff like Promise.in()...could get at it w/NativeCall if you really wanted but then you have to make it work on both posix and windows again
raydiak: Actually, I was asking about alarm() simply because I wanted to use a Promise, but reading from STDIN in a Promise is a bug in Windows Raduko.
It makes a Promise object, but shouldn't "for lines" never return?
mickcy_ca: as for the consumer, i'm starting a Promise that contains the while which emits - i think .list or .from-list could work as mentioned with gather/take, but i didn't figure out how that'd work
Promise tries to type-check its arguments, but doesn't success
Does it break the Promise, or does it keep the Promise but the result is a Failure?
suspects he's observing Promise.allof spinlocking or some such. 100% CPU usage, but no input is coming in :-/
I'm very, very confused. If I do «await Promise.anyof(@threads, Promise.in($short))», and in one of those threads call sleep($long) or Promise.in($long) (or anything long-running?), the anyof doesn't return for the Promise.in($short) but blocks on the entire thing...
In S17 the documentation for a Promise refers to the excuse method, but Promise.pm defines cause.
method Promise() { my $l = Lock.new; my $p = Promise.new; my $v = $p.vow; my $t = self.tap(-> \val { $l.protect({ if $p.status == Planned { $v.keep(val); $t.close() } }); }, done => $v.break("No value received"), quit => -> \ex { ...similar to more code but .break }); $p }
or do we need to change Promises into Channels, and use the .closed attribute on the Channel
the only thing i could imagine is Promise.care giving a handler that you can call .forget on and when the last .care has been .forget'd it would redeem ti, but that seems fragile and racy
lizmat: You need to await that Promise.allof, but maybe somebody managed to break allof too...
Promises are a bit of both. You can poll for the result, but .then is reactive.
TimToady: I originally had it as Promise.sleep, but lizmat wasn't keen on that...I can kinda see why-ish...
japhb__: Promise is getting a fairly big refactor but it's largely "on the inside" so I'm hopefully it'll not regress anything you're doing.
There will also be a way to get a "keeper" object from a Promise that lets you encapsulate the right to keep/break it, but it's optional
Thinking about it... The "Promise" analogy works well for read operations, but gets slightly weird for write operations...
jnthn: so Promise.sleep(1) makes a promise that doesn't do anything, but takes a second to do it?
Ulti: It's a Promise, but when you try to use the result it throws the exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment