-
-
Save makerj/30c202a3be153356915fe15f629cb8c5 to your computer and use it in GitHub Desktop.
email notifier using multiprocessing(manager process, worker process[pool])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import random | |
from multiprocessing import Queue, Process, Pool | |
from time import sleep | |
class EmailNotifier: | |
def __init__(self): | |
self.r_queue = Queue() | |
self.manager_proc = Process(target=EmailNotifier.init_manager_proc, name='EmailNotifier', args=(self.r_queue,)) | |
self.manager_proc.start() | |
@staticmethod | |
def init_manager_proc(queue): | |
# runs in ManagerProcess !!!!!!!!!! | |
worker_pool = Pool() # If processes is None then the number returned by os.cpu_count() is used. | |
while True: | |
item = queue.get() | |
worker_pool.apply_async(EmailNotifier.send_email_if_need, args=(item,)) | |
@staticmethod | |
def send_email_if_need(mail_data): | |
# runs in ManagerProcess.Worker !!!!!!!!!! | |
if mail_data[3]: | |
print('This worker PID: {}'.format(os.getpid())) | |
print('Now sending...') | |
print(mail_data[0]) | |
# Expose Interfaces ----------------------------------------------------------------------------------------------- | |
def put(self, page_poster_info): | |
# runs in MainProcess !! | |
self.r_queue.put(page_poster_info) | |
def shutdown(self): | |
# runs in MainProcess !! | |
self.manager_proc.terminate() | |
def generate_fixture(): | |
fixtures = [ | |
['1@naver.com', 'comment notification', 'content', 'True'], | |
['2@naver.com', 'comment notification', 'content', 'True'], | |
['3@naver.com', 'comment notification', 'content', 'True'], | |
['4@naver.com', 'comment notification', 'content', 'True'], | |
] | |
return random.choice(fixtures) | |
if __name__ == '__main__': | |
en = EmailNotifier() | |
while True: | |
en.put(generate_fixture()) | |
sleep(random.random()/10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment