-
Notifications
You must be signed in to change notification settings - Fork 62
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
Add optional setting of QtPromise and .then() context #20
Comments
I'm not sure I'm getting a good picture of what you are trying to do. |
let me sketch it out a bit more: the API uses a callback to provide IO data. The callback is executed in a thread owned by the API, which is not These are basically two things combined:
I hope it is a bit clearer now |
You want all the So if I got your situation right, you have an external library with a The easiest way of modeling this is to create a promise, store the resolver in your callback function, call QPromise<Data> asyncApiCall() {
return QPromise<Data>([](const auto& resolve, const auto&) {
registerCallback([=](Data data) {
// in foreign thread
resolve(data);
});
startAsyncProcess();
});
} Now you simply initiate and await your process in main using |
Thanks @pwuertz, I like your approach, which is close to what it's done in the unit test. @egonuel does @pwuertz solution fix your issue? I'm not sure why you need a |
thank you both of you. sorry I did not get back to you earlier, I could not work on it until today. My problem is that the callback is called repeatedly, each time new data is available. As far as I understand a promise can only be fullfilled once. I implement @pwuertz code and it appears that my understanding is right: it is only called once.
Here |
So my prior code looks like this:
Now the new code:
so it's a lot more code, still I somehow like the approach. But I don't like that this requires to continuously set the callback and interact with the api thread with mutex etc. I'm playing with this and try to get more insights and maybe get it shorter and more concise. Maybe my design is also somehow flawed. I'm open for comments and ideas... |
Closing this issue in favor of #35 |
Hi,
I'm playing around with qtpromise and have situation where I'm launching a function using QtConcurrent:run() from an non-qt thread which is owned by an 3rdparty API. Using qtpromise in this situation appears not to be working, as the watcher in PromiseFulfill can not do its job without an eventloop and qtpromise_defer() would be using QThread::currentThread() which in my situation is the API thread also without an event loop.
I started to implement support for that, by adding a parameter to QtPromise which takes a pointer to a QThread which is passed down to PromiseFulfill::call(), where I move the watcher to that thread. In addition I added a parameter to .then() to also take a thread which is passed to the handlers and catchers which allow qtpromise_defer() to have a valid thread to post the event to.
Is that kind of functionality of general interest?
If so, what do you think would be better to use as a context, a thread or an object?
The text was updated successfully, but these errors were encountered: