Skip to content

Instantly share code, notes, and snippets.

@makersm
Created May 26, 2016 15:12
Show Gist options
  • Save makersm/e0ac7d14c41cd96023d94e354ad24ebb to your computer and use it in GitHub Desktop.
Save makersm/e0ac7d14c41cd96023d94e354ad24ebb to your computer and use it in GitHub Desktop.
email notifier using multiprocessing(manager process, worker process[pool])
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)
@makerj
Copy link

makerj commented May 26, 2016

Nice snippet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment