NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import queue
import threading
import multiprocessing
import time

# Message Processor Framework Class
class MessageProcessor:
def __init__(self, num_workers=1, use_multiprocessing=False):
self.message_queue = queue.Queue()
self.num_workers = num_workers
self.use_multiprocessing = use_multiprocessing

def process_message(self, message, process_func):
# Process the message using the specified processing function
result = process_func(message)
print(f"Processed Message: {message} -> {result}")

def worker(self, process_func):
# Worker function that continuously processes messages from the queue
while True:
message = self.message_queue.get()
if message is None:
break
self.process_message(message, process_func)
self.message_queue.task_done()

def start_workers(self, process_func):
# Start worker threads or processes based on configuration
if self.use_multiprocessing:
workers = [
multiprocessing.Process(target=self.worker, args=(process_func,))
for _ in range(self.num_workers)
]
else:
workers = [
threading.Thread(target=self.worker, args=(process_func,))
for _ in range(self.num_workers)
]

for worker in workers:
worker.start()

return workers

def process_stream(self, process_func, stream_func, num_messages=10):
# Entry point to start processing messages from a stream
workers = self.start_workers(process_func)

for _ in range(num_messages):
message = stream_func() # Generate a message from the stream
self.message_queue.put(message)

# Block until all messages are processed
self.message_queue.join()

# Stop workers by sending None to the queue
for _ in range(self.num_workers):
self.message_queue.put(None)

# Wait for all workers to complete
for worker in workers:
worker.join()

# Example: Processing Functions
def process_function_1(message):
# Example processing function 1
return message.upper()

def process_function_2(message):
# Example processing function 2
return message[::-1]

# Example: Stream Entry Point Function
def stream_messages():
# Simulating a stream of messages (e.g., from an API)
return str(time.time()) # Return a timestamp as a message

# Example: Using the MessageProcessor framework
if __name__ == "__main__":
# Initialize the MessageProcessor
processor = MessageProcessor(num_workers=2, use_multiprocessing=False)

# Process messages with different processing functions
print("Processing messages using function 1:")
processor.process_stream(process_function_1, stream_messages, num_messages=5)

print("nProcessing messages using function 2:")
processor.process_stream(process_function_2, stream_messages, num_messages=5)
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.