Not sure if I should use threading or not?

Viewed 84

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.

1 Answers

I would use threads if either of these are true:

  • The blocking I/O database calls in B can negatively impact A's ability to collect data in a timely manner.
  • These two data collection pieces together can negatively impact the responsiveness of other parts of the app (think unresponsive GUI)

If neither condition is true, then a single threaded app is a lot less hassle.

Consider using a Queue for concurrency if you do use threads. Class A can post data to a Queue that class B is waiting on. Here is a bare bones code example of what I mean:

from queue import Queue
from threading import Thread, Event

class class_a:
    def __init__(self, queue):
        self.queue = queue
        self.thread = Thread(target=self.collect_data)
        self.thread.start()

    def collect_data(self):
        for data in range(1000):
            if data % 3 == 0:
                print(f'Thread A sending {data} to queue')
                self.queue.put(data)
            else:
                print(f'Thread A discarding {data}')

class class_b:
    def __init__(self):
        self.queue = Queue()
        self.thread = Thread(target=self.process_data)
        self.thread.daemon = True
        self.thread.start()

    def process_data(self):
        while True:
            data = self.queue.get()
            print(f'Thread B received {data} from queue')

b = class_b()
a = class_a(b.queue)

Lastly, anytime you think about using parallelism in python, you have to ask whether multiprocessing makes more sense than multithreading. Multiprocessing is a better choice when CPU computation, rather than file or network I/O, becomes the limiting factor in the performance of the app. I don't think multiprocessing is a good fit for your project based on the information you provided.

Related