This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ThreadingProcess.py
80 lines (62 loc) · 2.14 KB
/
ThreadingProcess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /bin/python3
# Object List and Threading demo
# Good for explicit control over transmissions
# No actual Reticulum action in this version
import time
import threading
DataLink = None #This would contain a Reticulum link for resource transfer
ObjectList = []
ShouldRun = True
class ObjectOfInterest:
ObjName = "Undefined"
ObjClass = "Undefined"
ObjSerial = 0
def __init__(self,N, C, S):
self.ObjName = N
self.ObjClass = C
self.ObjSerial = S
def PrintOOI(OOI):
print(OOI.ObjName+" - "+OOI.ObjClass+". SN:"+str(OOI.ObjSerial))
def ProcessOOI(ReticulumLink, OOI):
#This simulates a five second transmission that would otherwise block processing
print("Processing SN:"+str(OOI.ObjSerial))
time.sleep(5)
PrintOOI(OOI)
print("Processing complete.")
def ProcessLoop(ReticulumLink, ListOfObjects):
#This thread will not terminate on its own or when the parent terminates (for some reason)
#The global ShouldRun is a master kill switch
global ShouldRun
while(ShouldRun):
if len(ListOfObjects) > 0:
ProcessOOI(ReticulumLink,ListOfObjects.pop(0))
time.sleep(1)
def MainLoop():
StartTime = time.time()
global ShouldRun
LoopNum = 0
ProcessingThread = threading.Thread(target=ProcessLoop,args=(DataLink,ObjectList,))
ProcessingThread.start()
while(ShouldRun):
DeltaTime = time.time() - StartTime
if DeltaTime > 30:
ShouldRun = False
print("Terminating - Time elapsed")
else:
print("Loop Number: "+str(LoopNum))
LoopNum = LoopNum + 1
time.sleep(1)
print("Loops should be approximately 29 - Significantly less indicates a blocking ProcessOOI")
print("OOI processed should be three.")
#Initialization
ObjectList.append(ObjectOfInterest("Testy","McTestface",1))
ObjectList.append(ObjectOfInterest("Foo","Bar",9))
ObjectList[1].ObjSerial = 2 #oops! Change that
#As an object
WorkingObject = ObjectOfInterest("Last","Test",4)
WorkingObject.ObjName = "Doing this"
WorkingObject.ObjClass = "The hard way"
WorkingObject.ObjSerial = 3
ObjectList.append(WorkingObject)
#Go
MainLoop()