I will try to keep what I am trying to do as simple as possible.
I have two classes ClassA and ClassB
ClassA has an instance method that contains a while loop that runs "infinitely" and collects data. ClassA is also passed an instance of ClassB. While ClassA collects this data, it is also checking the data that comes in to see if a certain signal has been received. If the signal has been received, an instance method in ClassB is called upon.
Consider the following main program driver:
from class_a import ClassA from class_b import ClassB database_connection = MongoDB #purely example class_b = ClassB(database_connection) class_a = ClassA(class_b) And then the classes:
Class class_a: def __init__(self, class_b): self.class_b def collect_data(self): while True: data = receiver() if (signal in data): self.class_b.send_data_to_database(data) Class class_b: def __init__(self, database): self.database = database def convert_data(self, data): return data + 1 def send_data_to_database(data): converted_data = convert_data(data) self.database.send(converted_data) Now here is my question. Should I have a thread for the "send_data_to_database()" instance method in Class B? My thought process is that possibly spawning a thread just to deal with sending data to a database, will be faster THAN the instance method NOT being threaded. Is my thinking wrong here? My knowledge of threading is limited. Ultimately, I am just trying to find the fastest way to send data to the database upon Class A recognizing that there is a signal in the data. Thanks to all of those who reply in advance.
... -> receive -> check -> send -> receive -> .... Offloading a single action to a thread, e.g. send, is generally not worth it - starting the thread takes longer than just doing the action directly.ClassAdoes not sleep between data collection runs. To keep things simple, this is pretty much the app (besides the data being processed). There is no significant risk to data collection falling behind; My main concern is being able to send the data as fast as possible upon receiving that signal.