Python Forum
Prevent using time.sleep() and multiprocessing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prevent using time.sleep() and multiprocessing
#1
Hi folks,

I'd like to share my concept about stopping particular functions out in Python, let them sleep for some time, while the whole script is still running. I had the task in my own personal project to stop one specific def() for 30 seconds but I didn't want to lose all data which was coming in while that time frame. I am not long time in the programing business and I don't know if there is something similar ( or better ) out there, but all I've found over the last 1 year was a) the concept of time.sleep() and b) the concept of multiprocessing.

Option a was ruled out for me, like I'd explained and option b was too complicated for me so I searched for something based on time ( seconds, minutes ).

Long story short, here is my script:

from time import time
from time import strftime

minute_list = []
second_list = []
app_ping = 0

minute_bool = False
second_over_60 = False
second_under_60 = False
Beneath is the function to control the time frame and the conditions. In a you only have to fill the time frame ( in seconds ) you need. This script is made for time frames <= 59 seconds. For longer frames It would have to be tweaked.

def minute_man(a=30):
	global minute_bool, second_over_60, second_under_60
	global app_ping

	if len(second_list) >= 2:
		if (second_list[0] + a) > 60:
				rest = 60 - second_list[0]
				rest_2 = a - rest
				if minute_bool is True and second_list[-1] >= rest_2:
					second_over_60 = True
					minute_list.clear()
					second_list.clear()
					app_ping = 0
					second_over_60 = False
					minute_bool = False
		elif (second_list[0] + a) < 60:
			if second_list[-1] >= (second_list[0] + a):
				second_under_60 = True
				minute_list.clear()
				second_list.clear()
				app_ping = 0
				second_under_60 = False
				minute_bool = False
	if len(minute_list) >= 2:
		if minute_list[0] == 59 and minute_list[-1] == 0:
			minute_bool = True
		elif minute_list[0] < 59 and (minute_list[-1] == (minute_list[0] + 1)):
			minute_bool = True
		else:
			minute_bool = False

	# print(minute_bool, second_over_60, second_under_60)
Simple function which has to do something in specific time frame.

def app():
	global app_ping

	current = strftime("%H:%M:%S")
	current_minute = strftime("%M")
	app_now = time()
	app_second = float(app_now % 60)

	if app_ping == 0:
		app_ping += 1
		minute_list.append(int(current_minute))
		second_list.append(app_second)
		print("done", "Time Stamp:", current)
The loop. I decided to chose milliseconds to get the most possible accuracy.

while True:
	now = time()
	second = float(now % 60)
	minute_time = strftime("%M")

	minute_man()
	app()

	if app_ping > 0:
		minute_list.append(int(minute_time))
		second_list.append(second)
The Output:

done Time Stamp: 21:39:39
done Time Stamp: 21:40:09
done Time Stamp: 21:40:39
That's all. Simple construction with options for endless functions to run simultaneously.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020