I have a loop with a delay which calls a method inside my class, however it only runs fully once, then only the first line gets called, I can see this through my logs in the terminal.
class Processor:
def __init__(self) -> None:
self.suppliers = ["BullionByPostUK", "Chards"]
self.scraper_delay = 60 # in seconds (30mins)
self.oc_delay = 30 # in seconds (5mins)
self.scraper = Scraper()
def run_one(self):
self.scraper.scrape()
self.data = self.scraper.get_data()
t_s = time.perf_counter()
for supplier in self.suppliers:
eval(supplier)(self.data).run() # danger? eval()
t_e = time.perf_counter()
dur = f"{t_e - t_s:0.4f}"
dur_ps = float(dur) / len(self.suppliers)
print(f"Processed/Updated {len(self.suppliers)} suppliers in {dur} seconds \n Average {dur_ps} seconds per supplier\n")
logging.info(f"Processed/Updated {len(self.suppliers)} suppliers in {dur} seconds \n Average {dur_ps} seconds per supplier")
def run_queue(self): # IN PROGRESS
print(color.BOLD + color.YELLOW + f"[{datetime.datetime.utcnow()}] " + color.END + f"Queue Started")
while True:
recorded_silver = self.scraper.silver()
try:
wait(lambda: self.bullion_diff(recorded_silver), timeout_seconds=self.scraper_delay, sleep_seconds=self.oc_delay)
self.run_one()
except exceptions.TimeoutExpired: # delay passed
self.run_one() # update algorithm (x1)
finally:
continue
...
As seen from image provided, each run through after does not run all the way up to process
Why is this that the code up to before the eval() only runs once then stops out? Im basically trying to loop the run_one() method so it runs on a cycle.
Well, I am making this program which will click a link sent on zoom and close zoom at the end of meeting
This is the code
import pyautogui
import time
import pause
import datetime
import schedule
YEAR = 2020
MONTH = 11
DATE = 6
HOUR = 10
MINUTES = 1
SECONDS = 00
now = datetime.datetime.now()
EndTime = now.replace(hour=HOUR, minute=MINUTES, second=SECONDS, microsecond=0)
JustBefore= now.replace(hour=HOUR, minute=MINUTES-1, second=SECONDS, microsecond=0)
def leave_the_meeting():
pyautogui.click(1198, 1072)
time.sleep(3)
pyautogui.click(1443, 998)
time.sleep(1)
pyautogui.click(1398, 933)
def click_the_attendance_link():
pyautogui.click(1665, 674)
time.sleep(9)
I want click_the_attendance_link() to keep functioning over and over again until JustBefore and then stop.
and then I want leave_the_meeting() to start functioning for just one time at EndTime.
What code should I add?
How about this:
def keep_click_attendance_link():
while datetime.datetime.now() < JustBefore:
click_attendance_link()
# Sleep for 60 secs before trying again
time.sleep(60)
while datetime.datetime.now() < Endtime:
# Sleep for 1 sec intervals:
time.sleep(1)
# eventually Leave the meeting at Endtime
leave_the_meeting()
I'm working on a Raspberry Pi (3 B+) making a data collection device and I'm
trying to spawn a process to record the data coming in and write it to a file. I have a function for the writing that works fine when I call it directly.
When I call it using the multiprocess approach however, nothing seems to happen. I can see in task monitors in Linux that the process does in fact get spawned but no file gets written, and when I try to pass a flag to it to shut down it doesn't work, meaning I end up terminating the process and nothing seems to have happened.
I've been over this every which way and can't see what I'm doing wrong; does anyone else? In case it's relevant, these are functions inside a parent class, and one of the functions is meant to spawn another as a thread.
Code I'm using:
from datetime import datetime, timedelta
import csv
from drivers.IMU_SEN0 import IMU_SEN0
import multiprocessing, os
class IMU_data_logger:
_output_filename = ''
_csv_headers = []
_accelerometer_headers = ['Accelerometer X','Accelerometer Y','Accelerometer Z']
_gyroscope_headers = ['Gyroscope X','Gyroscope Y','Gyroscope Z']
_magnetometer_headers = ['Bearing']
_log_accelerometer = False
_log_gyroscope= False
_log_magnetometer = False
IMU = None
_writer=[]
_run_underway = False
_process=[]
_stop_value = 0
def __init__(self,output_filename='/home/pi/blah.csv',log_accelerometer = True,log_gyroscope= True,log_magnetometer = True):
"""data logging device
NOTE! Multiple instances of this class should not use the same IMU devices simultaneously!"""
self._output_filename = output_filename
self._log_accelerometer = log_accelerometer
self._log_gyroscope = log_gyroscope
self._log_magnetometer = log_magnetometer
def __del__(self):
# TODO Update this
if self._run_underway: # If there's still a run underway, end it first
self.end_recording()
def _set_up(self):
self.IMU = IMU_SEN0(self._log_accelerometer,self._log_gyroscope,self._log_magnetometer)
self._set_up_headers()
def _set_up_headers(self):
"""Set up the headers of the CSV file based on the header substrings at top and the input flags on what will be measured"""
self._csv_headers = []
if self._log_accelerometer is not None:
self._csv_headers+= self._accelerometer_headers
if self._log_gyroscope is not None:
self._csv_headers+= self._gyroscope_headers
if self._log_magnetometer is not None:
self._csv_headers+= self._magnetometer_headers
def _record_data(self,frequency,stop_value):
self._set_up() #Run setup in thread
"""Record data function, which takes a recording frequency, in herz, as an input"""
previous_read_time=datetime.now()-timedelta(1,0,0)
self._run_underway = True # Note that a run is now going
Period = 1/frequency # Period, in seconds, of a recording based on the input frequency
print("Writing output data to",self._output_filename)
with open(self._output_filename,'w',newline='') as outcsv:
self._writer = csv.writer(outcsv)
self._writer.writerow(self._csv_headers) # Write headers to file
while stop_value.value==0: # While a run continues
if datetime.now()-previous_read_time>=timedelta(0,1,0): # If we've waited a period, collect the data; otherwise keep looping
print("run underway value",self._run_underway)
if datetime.now()-previous_read_time>=timedelta(0,Period,0): # If we've waited a period, collect the data; otherwise keep looping
previous_read_time = datetime.now() # Update previous readtime
next_row = []
if self._log_accelerometer:
# Get values in m/s^2
axes = self.IMU.read_accelerometer_values()
next_row += [axes['x'],axes['y'],axes['z']]
if self._log_gyroscope:
# Read gyro values
gyro = self.IMU.read_gyroscope_values()
next_row += [gyro['x'],gyro['y'],gyro['z']]
if self._log_magnetometer:
# Read magnetometer value
b= self.IMU.read_magnetometer_bearing()
next_row += b
self._writer.writerow(next_row)
# Close the csv when done
outcsv.close()
def start_recording(self,frequency_in_hz):
# Create recording process
self._stop_value = multiprocessing.Value('i',0)
self._process = multiprocessing.Process(target=self._record_data,args=(frequency_in_hz,self._stop_value))
# Start recording process
self._process.start()
print(datetime.now().strftime("%H:%M:%S.%f"),"Data logging process spawned")
print("Logging Accelerometer:",self._log_accelerometer)
print("Logging Gyroscope:",self._log_gyroscope)
print("Logging Magnetometer:",self._log_magnetometer)
print("ID of data logging process: {}".format(self._process.pid))
def end_recording(self,terminate_wait = 2):
"""Function to end the recording multithread that's been spawned.
Args: terminate_wait: This is the time, in seconds, to wait after attempting to shut down the process before terminating it."""
# Get process id
id = self._process.pid
# Set stop event for process
self._stop_value.value = 1
self._process.join(terminate_wait) # Wait two seconds for the process to terminate
if self._process.is_alive(): # If it's still alive after waiting
self._process.terminate()
print(datetime.now().strftime("%H:%M:%S.%f"),"Process",id,"needed to be terminated.")
else:
print(datetime.now().strftime("%H:%M:%S.%f"),"Process",id,"successfully ended itself.")
====================================================================
ANSWER: For anyone following up here, it turns out the problem was my use of the VS Code debugger which apparently doesn't work with multiprocessing and was somehow preventing the success of the spawned process. Many thanks to Tomasz Swider below for helping me work through issues and, eventually, find my idiocy. The help was very deeply appreciated!!
I can see few thing wrong in your code:
First thing
stop_value == 0 will not work as the multiprocess.Value('i', 0) != 0, change that line to
while stop_value.value == 0
Second, you never update previous_read_time so it will write the readings as fast as it can, you will run out of disk quick
Third, try use time.sleep() the thing you are doing is called busy looping and it is bad, it is wasting CPU cycles needlessly.
Four, terminating with self._stop_value = 1 probably will not work there must be other way to set that value maybe self._stop_value.value = 1.
Well here is a pice of example code based on the code that you have provided that is working just fine:
import csv
import multiprocessing
import time
from datetime import datetime, timedelta
from random import randint
class IMU(object):
#staticmethod
def read_accelerometer_values():
return dict(x=randint(0, 100), y=randint(0, 100), z=randint(0, 10))
class Foo(object):
def __init__(self, output_filename):
self._output_filename = output_filename
self._csv_headers = ['xxxx','y','z']
self._log_accelerometer = True
self.IMU = IMU()
def _record_data(self, frequency, stop_value):
#self._set_up() # Run setup functions for the data collection device and store it in the self.IMU variable
"""Record data function, which takes a recording frequency, in herz, as an input"""
previous_read_time = datetime.now() - timedelta(1, 0, 0)
self._run_underway = True # Note that a run is now going
Period = 1 / frequency # Period, in seconds, of a recording based on the input frequency
print("Writing output data to", self._output_filename)
with open(self._output_filename, 'w', newline='') as outcsv:
self._writer = csv.writer(outcsv)
self._writer.writerow(self._csv_headers) # Write headers to file
while stop_value.value == 0: # While a run continues
if datetime.now() - previous_read_time >= timedelta(0, 1,
0): # If we've waited a period, collect the data; otherwise keep looping
print("run underway value", self._run_underway)
if datetime.now() - previous_read_time >= timedelta(0, Period,
0): # If we've waited a period, collect the data; otherwise keep looping
next_row = []
if self._log_accelerometer:
# Get values in m/s^2
axes = self.IMU.read_accelerometer_values()
next_row += [axes['x'], axes['y'], axes['z']]
previous_read_time = datetime.now()
self._writer.writerow(next_row)
# Close the csv when done
outcsv.close()
def start_recording(self, frequency_in_hz):
# Create recording process
self._stop_value = multiprocessing.Value('i', 0)
self._process = multiprocessing.Process(target=self._record_data, args=(frequency_in_hz, self._stop_value))
# Start recording process
self._process.start()
print(datetime.now().strftime("%H:%M:%S.%f"), "Data logging process spawned")
print("ID of data logging process: {}".format(self._process.pid))
def end_recording(self, terminate_wait=2):
"""Function to end the recording multithread that's been spawned.
Args: terminate_wait: This is the time, in seconds, to wait after attempting to shut down the process before terminating it."""
# Get process id
id = self._process.pid
# Set stop event for process
self._stop_value.value = 1
self._process.join(terminate_wait) # Wait two seconds for the process to terminate
if self._process.is_alive(): # If it's still alive after waiting
self._process.terminate()
print(datetime.now().strftime("%H:%M:%S.%f"), "Process", id, "needed to be terminated.")
else:
print(datetime.now().strftime("%H:%M:%S.%f"), "Process", id, "successfully ended itself.")
if __name__ == '__main__':
foo = Foo('/tmp/foometer.csv')
foo.start_recording(20)
time.sleep(5)
print('Ending recording')
foo.end_recording()
I am new here and I was hoping someone could help me out with a project I'm attempting to create. Using Python I would like to make a countdown clock from user-specific information that, when getting a month day and year, would finish by printing an active countdown clock for that specific date. Is this possible? If so, how?
Thanks in advance.
-Anthony
The code below can be a start. The function date_countdown takes in a date string and a date format corresponding to the date string, and outputs a countdown to the terminal.
countdown.py
import datetime
def date_countdown(future_datetime, date_format):
print("Countdown for {}:".format(future_datetime))
seconds_left = -1
while seconds_left:
cur = datetime.datetime.now()
fut = datetime.datetime.strptime(future_datetime, date_format)
left = fut - cur
print(
"{0: >3d} days, {1:0>2d}:{2:0>2d}:{3:0>2d}".format(
left.days, # days
left.seconds // 3600, # hours
(left.seconds // 60) % 60, # minutes
(left.seconds % 60) # seconds
),
end='\r')
time.sleep(1)
seconds_left = left.total_seconds()
print('Done!')
date_countdown('2018-12-25', '%Y-%m-%d')
Output:
Countdown for 2018-12-25:
27 days, 04:15:40
I need my script to download a new file, if the old one is old enough. I set the maximum age of file in seconds. So that I would get back on track with my script writing I need example code, where file age is printed out in seconds.
This shows how to find a file's (or directory's) last modification time:
Here are the number of seconds since the Epoch, using os.stat
import os
st=os.stat('/tmp')
mtime=st.st_mtime
print(mtime)
# 1325704746.52
Or, equivalently, using os.path.getmtime:
print(os.path.getmtime('/tmp'))
# 1325704746.52
If you want a datetime.datetime object:
import datetime
print("mdatetime = {}".format(datetime.datetime.fromtimestamp(mtime)))
# mdatetime = 2012-01-04 14:19:06.523398
Or a formated string using time.ctime
import stat
print("last accessed => {}".format(time.ctime(st[stat.ST_ATIME])))
# last accessed => Wed Jan 4 14:09:55 2012
print("last modified => {}".format(time.ctime(st[stat.ST_MTIME])))
# last modified => Wed Jan 4 14:19:06 2012
print("last changed => {}".format(time.ctime(st[stat.ST_CTIME])))
# last changed => Wed Jan 4 14:19:06 2012
Although I didn't show it, there are equivalents for finding the access time and change time for all these methods. Just follow the links and search for "atime" or "ctime".
Another approach (I know I wasn't the first answer but here goes anyway):
import time, os, stat
def file_age_in_seconds(pathname):
return time.time() - os.stat(pathname)[stat.ST_MTIME]
The accepted answer does not actually answer the question, it just gives the answer for last modification time. For getting the file age in seconds, minutes or hour you can do this.
import os, time
def file_age(filepath):
return time.time() - os.path.getmtime(filepath)
seconds = file_age('myFile.txt') # 7200 seconds
minutes = int(seconds) / 60 # 120 minutes
hours = minutes / 60 # 2 hours
Use stat.M_TIME to get the last modified time and subtract it from the current time.
http://docs.python.org/library/stat.html
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os, time
def file_age_in_seconds(filename):
try:
return int(time.time() - os.path.getmtime(filename))
except:
#on any failure condition
return -1
filename = "/tmp/foobar.txt"
print(file_age_in_seconds(filename)) #prints -1
f = open(filename, 'w')
f.write("this is a line")
f.close()
print(file_age_in_seconds(filename)) #prints 0
time.sleep(4.2)
print(file_age_in_seconds(filename)) #prints 4
This will do in days, can be modified for seconds also:
#!/usr/bin/python
import os
import datetime
from datetime import date
t1 = os.path.getctime("<filename>")
now = datetime.datetime.now()
Y1 = int(datetime.datetime.fromtimestamp(int(t1)).strftime('%Y'))
M1 = int(datetime.datetime.fromtimestamp(int(t1)).strftime('%m'))
D1 = int(datetime.datetime.fromtimestamp(int(t1)).strftime('%d'))
date1 = date(Y1, M1, D1)
Y2 = int(now.strftime('%Y'))
M2 = int(now.strftime('%m'))
D2 = int(now.strftime('%d'))
date2 = date(Y2, M2, D2)
diff = date2 - date1
days = diff.days
You can get it by using OS and datetime lib in python:
import os
from datetime import datetime
def fileAgeInSeconds(directory, filename):
file = os.path.join(directory, filename)
if os.path.isfile(file):
stat = os.stat(file)
try:
creation_time = datetime.fromtimestamp(stat.st_birthtime)
except AttributeError:
creation_time = datetime.fromtimestamp(stat.st_mtime)
curret_time = datetime.now()
duration = curret_time - creation_time
duration_in_s = duration.total_seconds()
return duration_in_s
else:
print('%s File not found' % file)
return 100000
#Calling the function
dir=/tmp/
fileAgeInSeconds(dir,'test.txt')