Hello and excuse my English.
My coding is also crapy… and this is why I’m writing. It doesn’t work.
Let me explain:
In this code a random file is picked up in a folder full of scrollphat’s python codes then execute for 30 seconds and looped again. But when the picked file is executed she doesn’t leave the main program to close it and loop again. I really don’t know why.
#!/usr/bin/env python
# coding: utf-8
import scrollphathd # Scrollphathd module from Pimoroni
import time
import random
import os
import thread
import threading
"""Randomopener.py: Open randomly a py files every desired period"""
print("\n"
"Randomopener\n"
"\n"
"Open a scrollphathd python file every desired period\n"
"\n"
"Press Ctrl+C to exit!\n"
"\n")
path = "files/" # Path to the list of python files
class Randomiser(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = False
def run(self): # Code a executer dans le thread
self.running = True
while self.running:
scrollphathd.clear() # Clear the scrollphathd screen
scrollphathd.show()
randomfile = random.choice(os.listdir(path)) # Randomiser
result = path + randomfile # Path and file
print (result)
exec(open(result).read(), globals()) # Execute the randomly selected file
def stop(self):
self.running = False
randomiser = Randomiser()
while True:
randomiser.start() # Demarre le thread
time.sleep(30) # Durée du thread
randomiser.stop() #Arret du thread
randomiser.join()
Finally I made it!
I change the threading part for multiprocessing and it’s much more better!
So the only thing you have to do is to put all your scrollphathd py scripts in a folder, change the folder path in the code, set the timer and launch all that.
Here is the code:
#!/usr/bin/env python
# coding: utf-8
import time
import random
import os
import multiprocessing
import sys
import scrollphathd # Scrollphathd module from Pimoroni
"""Randomopener.py: Open randomly a py files every desired period"""
__author__ = "Khabes"
__copyright__ = "Copyright 2017, The ScrollphatProject"
__credits__ = ["Khabes"]
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Khabes"
__email__ = "khabes@gmail.com"
__status__ = "In works"
print("\n"
"Randomopener\n"
"\n"
"Open a scrollphathd python file every desired period\n"
"\n")
path = "files/" # Path to the list of python files
running = True # First statement of the randomiser while fonction
def randomiser(): # Randomiser Program
while running:
scrollphathd.clear() # Clear the scrollphathd screen
scrollphathd.show()
randomfile = random.choice(os.listdir(path)) # Randomiser
result = path + randomfile # Path and file
print('Chosen file is ' + result + "\n"
"Launching\n")
exec(open(result).read(), globals()) # Execute the selected file
def timer(): # Timer Program
for i in range(30, 0, -1): # Set the timer to 30 seconds
time.sleep(1)
sys.stdout.write(str("-")) # Print the timer
sys.stdout.flush()
running = False # Changing the randomiser while condition
if __name__ == "__main__":
randomiserproc = multiprocessing.Process(target=randomiser)
timerproc = multiprocessing.Process(target=timer)
while True:
timerproc.start() # Launch the timer process
randomiserproc.start() # Launch the randomiser process
timerproc.join() # Wait for the end of the timer process
randomiserproc.terminate() # Close the randomiser process
timerproc.terminate() # Close the timer process
randomiserproc = multiprocessing.Process(target=randomiser)
timerproc = multiprocessing.Process(target=timer)
print("\n"
"\n"
"Time is finished.\n")