Interrupting a Function Call on Android

20 views
Skip to first unread message

ealaf...@gmail.com

unread,
Nov 22, 2014, 3:40:47 PM11/22/14
to kivy-...@googlegroups.com
So I am trying to include a way to force a function in my app to time out and terminate gracefully if it doesn't return in a given amount of time. I'm hoping there is a way to do this easily that I am just overlooking. I examined the Clock module of kivy, but it looked like most of the function args named 'timeout' were just telling the Clock how far out to schedule the function call.

What I am currently doing that works on my computer, but not on Android, is using the python multiprocess module and its Pool class. So I have something like

# Just a single worker process
pool = Pool(1)
result = pool.apply_async(myfunc)
timeout = 20.0

try:
    res = result.get(timeout=timeout)
    ...
except TimeoutError as e:
    print "Took longer than 20 seconds in the function, aborting!"
pool.close()


My problem is that even after fiddling with getting the multiprocessing library into my app distribution, it turns out that the Android OS has a broken implementation of semaphores (that's what the error message led me to believe at least).

The function that can take such a long time is from Sympy, and it usually means it won't be returning meaningful results anyway. If there is a way to handle this kind of interrupt by modifying the actual function that would be fine with me too since I have access to the source.

Thanks in advance, and any help or guidance would be much appreciated!

Alexander Taylor

unread,
Nov 23, 2014, 8:43:15 AM11/23/14
to kivy-...@googlegroups.com
I think multiprocessing just doesn't work on android, are you able to do something similar with threads instead?

ealaf...@gmail.com

unread,
Nov 24, 2014, 2:38:04 PM11/24/14
to kivy-...@googlegroups.com
I spent most of the day yesterday trying to figure out how to do what I wanted using threads, but I didn't have much luck. The best I got was that the app could regain control from the child thread (sympy function call) after the timeout period and continue with something else, but the child thread would remain alive. The problem with that is the thread will continue to consume resources until the sympy function returns normally, and in some cases this would bring my entire computer to a crawl. I think I'll just file this one in my TODOs for now...

Anyway, thanks for the suggestion! 
Reply all
Reply to author
Forward
0 new messages