0

I'm working on a project that's going to be handling thousands of accounts at a time with a limit of x workers at a time. I have a Handler class that's going to be the core of the program.

When the Handler class initiates it creates a new IMAP process that monitors an email inbox for incoming mail. It also checks to see how many accounts are currently created that are stored locally, if accounts created are < x, it starts x workers running an account creator and stores the created accounts locally.

When the IMAP process receives a new email it checks the contents of email for specific processing. It should alert the Handler process what to do with the received email.

I'm still new to threading, and especially multiprocessing, and used a queue from the parent thread to send commands to the child thread. However, in this use-case I need the child IMAP process to be able to communicate to the Handler class to tell it when a new email arrives and what to do when it's received.

How can I initiate x workers with multiprocessing while being able to communicate child->parent and child->child?

example of my IMAP class, using the same format for my other classes

class IMAP(object):
    def __init__(self):
        self._config = configparser.ConfigParser().read('config.ini')['imap']
        self._mail = imaplib.IMAP4_SSL(self._config['host'])
        self._mail.login(self._config['username'], self._config['password'])
        self._mail.list()

    def run(self):
        while True:
            # login to email and monitor

EDIT I apologize for confusion, I'm bad at describing my thoughts. I'm using multi-processing, not threading. Here's a rough diagram (x for account processing creation is x workers) enter image description here

TehPirate
  • 154
  • 1
  • 12
  • Wouldn't it be more efficient to let the child do whatever is needed? Otherwise the handler could become overloaded. – Thomas Weller Aug 22 '21 at 18:34
  • Threads and processes are two different constructs for concurrency; you can hardly be using both. Please [edit] to clarify this. – tripleee Aug 22 '21 at 18:40
  • @ThomasWeller yes except for the fact the project is very dynamic which would render a lot of processes running idle. Rather than initiating them as needed – TehPirate Aug 22 '21 at 18:43
  • @tripleee: why not? Shouldn't be a problem. – Thomas Weller Aug 22 '21 at 18:44
  • Maybe you should draw a small diagram on what you want to achieve. – Thomas Weller Aug 22 '21 at 18:46
  • Does this answer your question? [Multiprocessing - Pipe vs Queue](https://stackoverflow.com/questions/8463008/multiprocessing-pipe-vs-queue) – Thomas Weller Aug 22 '21 at 18:47
  • *If* the OP is indeed using both, that needs to be clarified, too; what's the relationship between threads and processes? But especially if they are new to concurrent programming, thatws probably rather too much to chew. I'm guessing this is actually only about multiprocesing. – tripleee Aug 22 '21 at 18:53
  • I've added a rough diagram to the OP, sorry for the confusion – TehPirate Aug 22 '21 at 19:01

0 Answers0