Forum Archive

Threading While Loop Crash

robertiii

So pardon my ignorance. I have a script with us and it works great....until it crashes the entire app. Hahaha.

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import threading
import os
import time
import ui

port = 2121

def getLocalIP():
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        s.connect(('google.com', 80))
        ip = s.getsockname()[0]
        s.close()
    except:
        ip = 'N/A'
    return ip

def imageSetter(valid_extensions=('jpg','jpeg','png')):
    while True:
        currentImage = ''
        set = False
        dirpath = os.path.expanduser('~/Documents/tetherShootPhotos')
        try:
            valid_files = [os.path.join(dirpath, filename) for filename in os.listdir(dirpath)]
            valid_files = [f for f in valid_files if '.' in f and f.rsplit('.',1)[-1] in valid_extensions and os.path.isfile(f)]

            if not valid_files:
                raise ValueError("No valid images in %s" % dirpath)

            imageLink = max(valid_files, key=os.path.getmtime)

            if imageLink != currentImage:
                newImage = ui.Image.named(imageLink)
                currentImage = imageLink
                v['image'].image = newImage
                v['ipAddress'].hidden = True
                v['connectAt'].hidden = True
                set = False
        except:
            if set == False:
                set = True
                newImage = ui.Image.named('tetherShoot.png')
                v['image'].image = newImage
                v['ipAddress'].hidden = False
                v['connectAt'].hidden = False
                ipAddr = getLocalIP()+':'+str(port)
                v['ipAddress'].text = ipAddr
        time.sleep(1)

def main():
    authorizer = DummyAuthorizer()
    authorizer.add_anonymous(os.path.expanduser('~/Documents/tetherShootPhotos'), perm='elradfmwM')
    handler = FTPHandler
    handler.authorizer = authorizer
    server = FTPServer(('0.0.0.0', port), handler)
    t = threading.Thread(target=server.serve_forever)
    t.start()
    try:
        setup = False
        while True: pass
    except KeyboardInterrupt:
        server.close_all()

if __name__ == '__main__':
    import ui 
    v = ui.load_view()
    v['ipAddress'].hidden   = True
    v['connectAt'].hidden = True
    v['image'].content_mode = ui.CONTENT_SCALE_ASPECT_FIT
    imageUpdater = threading.Thread(target=imageSetter)
    imageUpdater.start()
    v.present('fullscreen',hide_title_bar=True)
    main()
robertiii

Weirdest part is the fact that there is no error log. It kills Pythonista with no log.

robertiii

I feel like an idiot. The variable set and setup was supposed to be one. It was running continually in the thread and causing a crash I assume because of taking too much space. Changed the variables to the same and no problem.

cvp

@robertiii We all make mistakes and the proof that you do not have to feel like an idiot is that you've found the reason alone 👍

robertiii

I am curious why no log was left behind...I even added a startup script to log problems and it always left nothing.

cvp

@robertiii It happens when you use a lot of RAM, crash without any log...

westernira

I think your tread is still running in the UI threadpool and causes the UI to lockup.

Android will kill your thread if you lock the UI for ~2-3 seconds. AsyncTask will also allow you to display a status bar or spinning ring while the runnable does the work.