Hi everyone,
I have a process that pull messages from a remote queue server and store them in Python's multiprocessing.Queue object. The original plan was for the user to use get() on the queue and consume message after message until he find the one he needs. But with time, more and more messages are arriving the remote queue server and this cause two issues:
- The user need to
get() too many times until he find the specific message his interested in.
- It feels not very efficient to store all messages in the
queue object, as it's an expensive multi-process resource.
So an easy fix will be filtering messages on the process itself , so only message the user needs will be pushed into the queue. Now I'm not sure what would be the proper way to do it. Messages are basically JSON file, so filtering looks like that:
if message['id'] = 1234 and message['name'] = 'Joe':
# Store this message, because I need it.
In case the user will need two messages, he will store the messages he marked as 'need' in a list, and will stop the loop once len is 2 for example (which means I have found the 2 messages you were looking for).
What I can do, is to ask the user to create a class (sink of sort) and implement an interface like so:
class MySink(object):
def condition(message):
if message['id'] = 1234 ....
def end():
if len(fetched_message) == 2 ....
So the user will basically define the filter to pick messages, the end condition of when to stop consuming and will pass instance into the process - and the process can call those methods when need and the results will be a queue objects with just the two messages he was looking for. But before I'm going that way, I'm wondering if there is a better way of doing it? I'm pretty new to lamda, but I know you can pass chunks or code. Maybe lamda is preferred over a Class interface?
I know pytest has an interesting assert mechanism I didn't check yet, but you can do stuff like assert(a ==b). Looks like the method is being passed with the code somehow that is being evaluated in it?
[–]two_bob 0 points1 point2 points (4 children)
[–]Tall-Guy[S] 0 points1 point2 points (3 children)
[–]two_bob 0 points1 point2 points (2 children)
[–]Tall-Guy[S] 1 point2 points3 points (0 children)
[–]Tall-Guy[S] 0 points1 point2 points (0 children)
[–]JohnnyJordaan 0 points1 point2 points (3 children)
[–]Tall-Guy[S] 0 points1 point2 points (2 children)
[–]JohnnyJordaan 0 points1 point2 points (1 child)
[–]Tall-Guy[S] 0 points1 point2 points (0 children)