[PySide] PySide Digest, Vol 83, Issue 13

Ognyan Moore ognyan.moore at gmail.com
Mon Dec 9 19:05:41 CET 2019


I think it would be great if we can have some guidance regarding
multi-threading with CPU heavy tasks, not just IO bound tasks.  Say I
wanted to multi-thread a function


def cpu_intensive_function(a: int, b: int, c: int) -> np.ndarray:
    return some_numpy_function_calls(a, b, c)


How would I run this function in such a way that the computation does not
hang the GUI thread?  I have attempted to follow the guidance on this blog
post here:
https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
.
Where I utilize QObject.moveToThread(some_thread) as that article suggests,
the function still executes in the GUI thread.

Some caveats here being that I am making an attempt to have no references
to mutable objects in the GUI thread and the thread doing the computation.

Thanks!
Ogi


On Mon, Dec 9, 2019 at 3:00 AM <pyside-request at qt-project.org> wrote:

> Send PySide mailing list submissions to
>         pyside at qt-project.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://lists.qt-project.org/listinfo/pyside
> or, via email, send a message with subject or body 'help' to
>         pyside-request at qt-project.org
>
> You can reach the person managing the list at
>         pyside-owner at qt-project.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of PySide digest..."
>
>
> Today's Topics:
>
>    1. Re: Keeping GUI responsive (Sivan Greenberg)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sun, 8 Dec 2019 17:54:10 +0200
> From: Sivan Greenberg <sivan at omniqueue.com>
> To: Israel Brewster <ijbrewster at alaska.edu>
> Cc: pyside at qt-project.org
> Subject: Re: [PySide] Keeping GUI responsive
> Message-ID:
>         <
> CAJNcf-aV+Xygpo6GV+Kr-neifnEdTiWg3GYyLYg16nJQJbRhhA at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Hi Israel,
>
>  If your non main function is mostly IO bound, you could try implement your
> processing function with an interface that allows for polling of results
> using co-routines which is very slim and very robust (I used that in the
> past for several PyQt & PySide small apps but also for a full blown project
> for Ubuntu back in 2006[0] , though with PyGTK - but principle is same) and
> will allow your GUI main loop to be in charge, even if you ever need to use
> a spawned shell command or program! (see links below)
>
>  It seems that nowadays, there are even ready made libs for that (this one
> seems very interesting): https://github.com/reclosedev/async_gui and also
> supports abstraction over multiprocessing which would support you even if
> the the subtask is indeed CPU intensive (like data cleansing , validation
> etc).
>
>  If you're using python 3 , the whole coroutines interface has been greatly
> simplified, and could be easier to use, however the first guide will
> explain in deep and show how to achieve cooperative multitasking using
> plain Python 2.x interface:
> -
>
> http://masnun.com/2015/11/13/python-generators-coroutines-native-coroutines-and-async-await.html
> -
>
> https://medium.com/velotio-perspectives/an-introduction-to-asynchronous-programming-in-python-af0189a88bbb
>
> HTH,
>
> -Sivan
>
> [0]:
>
> http://www.ubuntugeek.com/hubackup-backup-application-for-ubuntu-home-users.html
>
> On Thu, Dec 5, 2019 at 1:41 AM Israel Brewster <ijbrewster at alaska.edu>
> wrote:
>
> > I know this is a FAQ, however I haven’t been able to make any of the
> > standard answers work for me. Here’s the situation:
> >
> > - using PySide2 5.12.2
> > - I have an object (QMainWindow subclass) that contains most of the code
> > for my application
> > - One of the functions that runs in response to user input takes around 2
> > seconds to run. The GUI obviously freezes during this time (BAD!)
> > - Said function needs to access and modify several large variables
> (pandas
> > data frames) from the main object
> >
> > So here’s the problem: If I run this function as a separate (python)
> > thread, that doesn’t help - the GUI is still frozen. I’m thinking this is
> > due to the GIL, but I could be wrong about that. Running under the
> > multiprocessing module, however, doesn’t appear to be an option due to
> the
> > number and size of the data structures that the function needs to modify,
> > and if I try just to see what happens, the process actually crashes.
> >
> > So what are my options here? How can I keep the GUI responsive while this
> > function runs, without being able to spin it off as a separate process?
> Or
> > is the only option going to be to completely rip apart the function and
> try
> > to re-build it in such a way that it can, somehow, still access the
> memory
> > from the main thread, while doing the processing in a separate function?
> > ---
> > Israel Brewster
> > Software Engineer
> > Alaska Volcano Observatory
> > Geophysical Institute - UAF
> > 2156 Koyukuk Drive
> > Fairbanks AK 99775-7320
> > Work: 907-474-5172
> > cell:  907-328-9145
> >
> > _______________________________________________
> > PySide mailing list
> > PySide at qt-project.org
> > https://lists.qt-project.org/listinfo/pyside
> >
>
>
> --
> -Sivan
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://lists.qt-project.org/pipermail/pyside/attachments/20191208/fc69df57/attachment-0001.html
> >
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> https://lists.qt-project.org/listinfo/pyside
>
>
> ------------------------------
>
> End of PySide Digest, Vol 83, Issue 13
> **************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20191209/3f54bd2c/attachment.html>


More information about the PySide mailing list