Pythonista-Issues icon indicating copy to clipboard operation
Pythonista-Issues copied to clipboard

How to make pythonista run in the background

Open yanglipmed opened this issue 6 years ago • 2 comments

Hi, how to make pythonista run in the background? for example, collect data from sensor. It seems that threading module is suspended and stop working temporarily when using other apps. multiprocessing module doesn't work.

IPhone -- Setting -- General --Background App Refresh doesn't have an item for Pythonista, but for other apps.

yanglipmed avatar Nov 25 '18 13:11 yanglipmed

Please take these discussions to the forum to avoid cluttering github with unsolvable issues. There is already plenty of threads on the topic in the forums, and the short answer is that you can not reliably run Pythonista in the background for any length of time.

Please close the issue.

mikaelho avatar Nov 25 '18 16:11 mikaelho

Edit: This may not work in all cases or may have a background time limit

After many hours of research I found that the app Pyto does this. I still wanted to use Pythonista so I re-created the method that Pyto used to keep the background process alive. The following may work

import sound
import os
import urllib
import time

class BackgroundTask:
	def __init__(self):
		if os.path.exists('silence.wav'):
			self.player = sound.Player('silence.wav')
		
	def run(self):
		self.player.number_of_loops = -1
		self.player.play()
		
	def stop(self):
		self.player.stop()

counter = 0
while(counter < 10):
	counter = counter + 1
	print(f'{counter}')
	time.sleep(1)

This also involves having a silent audio file called ‘silence.wav’ in the same folder of this class

Reference: https://github.com/ColdGrub1384/Pyto/blob/main/Pyto/Model/Background/BackgroundTask.swift

MBogushefsky avatar Aug 20 '22 19:08 MBogushefsky