[Mobile-sig] Pythonic idioms on mobile platforms

Russell Keith-Magee russell at keith-magee.com
Wed Dec 13 19:30:41 EST 2017


To clarify - are you talking about threading specifically, or async, or just the general problem of implementing long-lived behavior in a GUI without blocking the GUI thread?

If you’re talking about async, I’ve been able to integrate the Cocoa/iOS event loops with Python’s asyncio event loop with remarkably little code: 

https://github.com/pybee/rubicon-objc/blob/master/rubicon/objc/eventloop.py <https://github.com/pybee/rubicon-objc/blob/master/rubicon/objc/eventloop.py>

I haven’t actually tried to do this on Android yet, but from research I’ve done, it should be possible to use Handlers to implement the same thing. You don’t (necessarily) need threads - you just need a non-blocking (usually select-based) I/O API.

The upshot of this is that you can make every even handler (e.g., button click handlers) asynchronous, which means they can be long lived without blocking the GUI thread. There are a couple of examples of this in the Toga examples repository

The Detailed List widget example uses an async handler to simulate a process retrieving data from a remote API
https://github.com/pybee/toga/blob/master/examples/detailedlist/detailedlist/app.py#L14 <https://github.com/pybee/toga/blob/master/examples/detailedlist/detailedlist/app.py#L14>

Beeliza uses an async handler to emulate response times from a virtual chat participant
https://github.com/pybee/toga/blob/master/examples/beeliza/beeliza/app.py#L11 <https://github.com/pybee/toga/blob/master/examples/beeliza/beeliza/app.py#L11>

Yours,
Russ Magee %-)

> On 14 Dec 2017, at 8:15 am, David Boddie <david at boddie.org.uk> wrote:
> 
> [Answering Russell as well, as I see his response to my original message.]
> 
> I can try to be more specific about the problems I found, though I'm not sure
> if I really know the best practices for threading in Android (or Python for
> that matter).
> 
> I was trying to lazily populate items in a scrolling list using instances
> of the AsyncTask class, which is a convenient way to do work in a background
> thread:
> 
> https://developer.android.com/reference/android/os/AsyncTask.html
> 
> I initially thought I was being clever and had found a nice way to handle
> processing using background threads. The problem is that, for many items, you
> hit a limit where creating new AsyncTasks fails, so the abstraction is quite
> brittle and you need to find a way to process a large number of items.
> 
> I experimented with using a regular Thread and using queue and communicating
> with the main GUI thread using a Handler. This worked fine, but doesn't scale
> very well. I ended up returning to the AsyncTask solution and working around
> the problem by using a trick familiar to users of desktop GUI frameworks:
> posting an event for later evaluation.
> 
> One of my points is that getting things done in the GUI tends to involve
> techniques that purists would frown on, yet this is something desktop GUI
> developers are familiar with. Getting something to work can involve details
> that we would prefer not to expose to the user. Given that there is usually
> a preferred way to do things with the platform APIs, does that mean that
> things like the threading module are effectively obsolete for developers of
> mobile GUIs?
> 
> Sorry not to be more specific. I'm still trying to wrap my head around some
> of these issues.
> 
> David
> 
> On Wednesday 13. December 2017 08.12.42 Guido van Rossum wrote:
>> Can you be more specific?
>> 
>> On Dec 13, 2017 8:04 AM, "David Boddie" <david at boddie.org.uk> wrote:
>>> I was looking at threading on Android and it occurred to me that perhaps
>>> others sidestep problems I encountered with native APIs by using Python's
>>> own
>>> threading features. Then I wondered if some developers prefer to stick to
>>> the
>>> platform native APIs for things like threading, and if they found it
>>> difficult to adapt to a non-Pythonic API.
>>> 
>>> Has anyone else looked at how well (or poorly) some of the idioms used in
>>> Python map to each of the underlying mobile platforms and frameworks, or
>>> are
>>> people just using the native APIs directly? The answer to this might save
>>> someone the effort of trying to reproduce the APIs of those standard
>>> Python
>>> libraries that aren't interesting to mobile developers.
>>> 
>>> David
>>> _______________________________________________
>>> Mobile-sig mailing list
>>> Mobile-sig at python.org
>>> https://mail.python.org/mailman/listinfo/mobile-sig
> _______________________________________________
> Mobile-sig mailing list
> Mobile-sig at python.org
> https://mail.python.org/mailman/listinfo/mobile-sig

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/mobile-sig/attachments/20171214/b70cce81/attachment-0001.html>


More information about the Mobile-sig mailing list