Perfect, thanks a bunch Justin!

On 25 March 2015 at 20:07, Justin Israel <[email protected]> wrote:

>
>
> On Thu, Mar 26, 2015 at 8:56 AM Marcus Ottosson <[email protected]>
> wrote:
>
>> Great minds think alike? :)
>>
>> You also use “dispatch” which sounds like another option; we’re
>> dispatching a function onto a thread that triggers a callback once finished.
>>
>> dispatch(expensive_function, callback=on_completed)
>>
>> I didn’t quite get the interface for your version, does it require a
>> subclass?
>>
>
> There are usage example right at the bottom of the gist. It gets used like
> a normal threadpool:
>
> ```python
> self._pool = CallbackThreadPool(4)
> ...
> self._pool.apply_async(action, (i, i), callback=callback)
> ```
>
> You don't have to subclass anything by default, since it will just direct
> the event to the "Invoker" instance. But you *can* direct it to specific
> objects which can handle the posted event specifically.
>
> Once you have a thread pool, you just use it. You can either have one
> thread pool for your app, or thread pool instances in your specific
> classes. Whatever you want. I found that I needed to weakref functionality
> in there, if you are dealing with bound methods. And having the thread pool
> is nice to keep a bounded amount of threads running in the app.
>
> Here’s what I’ve been doing previously and was looking to simplify:
>>
>> # From within a class
>> self.finished.connect(self.on_finished)
>> def worker():
>>     time.sleep(3)
>>
>>     # Relies on an existing signal in the caller
>>     self.finished.emit("result")
>> # And creates junk variables that will never get referenced.
>> thread = threading.Thread(target=worker)
>> thread.daemon = True
>> thread.start()
>>
>> Which is quite the many lines for such a simple thing; I’m refraining
>> from doing too much of it due to the visual clutter it causes, along with
>> having to make signals serve single-shot purposes, one per unique task more
>> or less.
>>
>> Are you using anything like this in production? I had never seen or used
>> anything like it before outside of JavaScript, but in there it’s everywhere
>> and is incredibly useful and intuitive and I find a need for it in quite a
>> few places.
>>
> I'm using this CallbackThreadPool in production, in one of my Qt
> applications. I use it to process non-gui stuff in the threadpool and then
> safely callback to GUI logic when it is done.
>
>
>> ​
>>
>> On 25 March 2015 at 19:11, Justin Israel <[email protected]> wrote:
>>
>>> This looks kind of similar to something I did a while back
>>>
>>> https://gist.github.com/justinfx/6183367
>>>
>>> We even both use the term "invoke"  :-)
>>> Mine uses a posted event to the main loop instead of signals and slots.
>>>
>>> On Thu, 26 Mar 2015 7:39 AM Marcus Ottosson <[email protected]>
>>> wrote:
>>>
>>>> Hi all,
>>>>
>>>> I threw something together on a whim and would like your opinion of it.
>>>>
>>>> Inspired by JavaScript and it’s use of callbacks for IO-bound functions:
>>>>
>>>> expensiveFunction("argument", function(result) {
>>>>     console.log("The results are: " + result);
>>>> })
>>>>
>>>> I did this.
>>>>
>>>> def callback(result):
>>>>     print "The results are: %s" % result
>>>>
>>>> invoke(expensive_function, callback)
>>>>
>>>> Code here:
>>>> https://gist.github.com/mottosso/c20df396f4ecc882b53c
>>>>
>>>> Question is, is there already a way of doing this? If not, what could
>>>> be made better, faster, stronger? I’m calling it “invoke” but I’m sure
>>>> there’s already an established term for it, do you know of any?
>>>>
>>>> Best,
>>>> Marcus
>>>> ​
>>>> --
>>>> *Marcus Ottosson*
>>>> [email protected]
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Python Programming for Autodesk Maya" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOD6jDvWtY39eBEbQVU95HB2T0wF89GHeXSAdHEnUn5yDA%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOD6jDvWtY39eBEbQVU95HB2T0wF89GHeXSAdHEnUn5yDA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Python Programming for Autodesk Maya" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>>
>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1QfFCxB0wUv-Z8kQefi-koshdUjLQZgwTZbx-juAW1uw%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1QfFCxB0wUv-Z8kQefi-koshdUjLQZgwTZbx-juAW1uw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>
>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> --
>> *Marcus Ottosson*
>> [email protected]
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Python Programming for Autodesk Maya" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODMVxFJFzdk6%2BEvRiQZte514-uwYZXPLTwT40vjLTaq%2BQ%40mail.gmail.com
>> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODMVxFJFzdk6%2BEvRiQZte514-uwYZXPLTwT40vjLTaq%2BQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0X0tnrCPzkhOkwO2YMUM_W2BA7svG-huz-zySq%3DTJy_g%40mail.gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0X0tnrCPzkhOkwO2YMUM_W2BA7svG-huz-zySq%3DTJy_g%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
*Marcus Ottosson*
[email protected]

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODE6bRYUTVWWUbVN3UBQuV%3DnwoDvpFd%3DRSYwY0pKEZORQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to