I've a second.py as library which is called by main.py. I want to call a function inside main.py when a loop in second.py trigger. Here is the basic structure of my code. (The whole code is much bigger, I just simplified it, so structure should not be changed)
# main.py
import second
class Main:
def __init__(self):
self.second_functions = second
self.data_from_second = None
def func1(self):
# Initiate something in second
print(self.second_functions.SecondClass.test1(self))
def func2(self, data):
# Do something with data
self.data_from_second = data
print(f"{data}")
Main().func1()
# second.py
import time
class SomeHandler:
pass
class SecondClass(SomeHandler):
def __init__(self):
self.something = "Something"
def test1(self):
# do some stuff
self.something = "Initiated"
print("Second Class Initiated")
while 1:
# !!! from this point I want to trigger func2() in running main.py
print("Trigger func2('some data')")
# Main.func2("DATA") #When I try someting like this, it returns "NameError: name 'Main' is not defined" which is normal.
time.sleep(3)
second.py is actualy a socket server. When client sends something, I need to pass that info to main.py. Currently main.py call a function inside second.py in interval to get datas which is not right way I believe.