Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I actually use promises? #18

Open
ttulttul opened this issue Feb 8, 2018 · 7 comments
Open

How do I actually use promises? #18

ttulttul opened this issue Feb 8, 2018 · 7 comments

Comments

@ttulttul
Copy link

ttulttul commented Feb 8, 2018

Can you guys please provide a practical example of using promises to use the API asynchronously? It's just super unclear, and nothing I'm trying is working.

@pkopac
Copy link
Contributor

pkopac commented Feb 9, 2018

Hi @ttulttul,

we have examples in the README - Usage. Further details can be found in the library documentation.
But basically, you just need to chain .then(handler) to receive responses and .catch(handler) to receive errors. Let us know, if you keep running into some specific error.

@ttulttul
Copy link
Author

ttulttul commented Feb 9, 2018

Thanks. I got this basically working. Now, I'm confused as to why the promises system seems to be running only one asynchronous request at a time. I was expecting things to go a lot faster, in other words.. Is there a way to control the level of parallelism?

@ttulttul
Copy link
Author

ttulttul commented Feb 9, 2018

I can sort of answer my own question here. No, is the simple answer. This library uses Promises, but does not tie into an asynchronous requests library such as grequests. Therefore, the HTTP requests are all blocking. That really sucks.

@pkopac
Copy link
Contributor

pkopac commented Feb 10, 2018

@ttulttul: I see what you mean. I've tested the library locally and you are right.
That is indeed unexpected 🤔
It seems that you could enable asynchronous scheduling with:

from promise import Promise, set_default_scheduler
from promise.schedulers.asyncio import AsyncioScheduler
...
set_default_scheduler(AsyncioScheduler())

From syrusakbary/promise#45

But I've tried their code example locally and it still worked synchronously for me. I'll have to look into this more deeply, but maybe you can try it in the meanwhile? Perhaps I'm doing something wrong.

@pkopac
Copy link
Contributor

pkopac commented Feb 10, 2018

Update: I found an ugly workaround, see syrusakbary/promise#45 (comment) I think the promise library has 2 bugs that cause it to fall back to synchronicity.

@chtseac
Copy link

chtseac commented Feb 10, 2018

Hello @pkopac, I don't have time to test it with my code, but have you tried using the syntax Promise.promisify(func)() instead of Promise.resolve("foo").then(func, None)? I think that this is a slightly less ugly workaround.

@pkopac
Copy link
Contributor

pkopac commented Feb 12, 2018

@chtseac: thanks for the hint with additional (). I tried the method, of course, but this didn't occur to me! Anyway, it doesn't work either. syrusakbary/promise#45 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants