Forum Archive

Data logging in the background

max72

I'm playing with Pythonista, and it's a very nice tool.
I found also many suggestion on the forum, thanks.
I'm trying now to do accelerometer logging.
Issue is when the phone idles logging stops.
Searching around it looks like there is no viable solution, but I found an hint about the use of
console.set_idle_timer_disabled(flag)

I placed it in the Today widget and ran it, but unfortunately it's not working.
Is there any hope?
I'm attaching the code.

Thanks in advance,
Massimo

#!python3
import appex, ui
import location
import motion
import csv
import time
import sys
import console
from functools import partial

count=0
i=0

#@ui.in_background
def avvialog(sender):
    global count
    global i
    console.set_idle_timer_disabled(True)
    motion.start_updates()
    location.start_updates()
    if (count<10):
        ui.delay(partial(avvialog,sender),1)
        label.text="partito"+str(i)
        i=i+1
        v.add_subview(label)
        a=motion.get_user_acceleration()
        b=location.get_location()
        with open('filelogc.csv','a') as csvfile:
            fh = csv.writer(csvfile,delimiter=',')
            fh.writerow([time.clock(),time.asctime(),b["latitude"],b["longitude"],b["speed"],(a[0]**2+a[1]**2+a[2]**2)**0.5])
    else:   
        motion.stop_updates()
        location.stop_updates()
        label.text="stop"
        v.add_subview(label)
        console.set_idle_timer_disabled(False)
        count=0
        i=0


def fermalog(sender):
    global count
    count=100

# button setup  
v = ui.View(frame=(0, 0, 300, 150))
label = ui.Label(frame=(0, 0, 150, 90), flex='lwh', font=('<System>', 24), alignment=ui.ALIGN_CENTER, name='result_label',text="in pausa")
v.add_subview(label)
button1 = ui.Button(title='start', font=('<System>', 24), flex='rwh', action=avvialog)
button1.frame = (0, 50, 100, 100)
button2 = ui.Button(title='stop', font=('<System>', 24), flex='rwh', action=fermalog)
button2.frame = (120, 50, 220, 100)
v.add_subview(button1)
v.add_subview(button2)  
appex.set_widget_view(v)


mikael

@max72, combining Today widget and long-term processing seems like asking for extra trouble. In a plain script disabling the idle timer works fine, as long as you set the setting in the main thread (due to a bug in the latest version).

What is your use case?

cvp

@mikael it seems he uses it in background thread ipo main thread

max72

Thanks!
I made wrong assumptions about the today widget and its implications..
Running the script directly seems to solve the issues.
I'll give it a long-term whirl, but it looks like it works fine.
Thanks again for your time and insight.
Massimo