[Pymilter] Dataproxy proxy in pymilter
Stuart D Gathman
stuart at bmsi.com
Mon May 13 15:21:36 EDT 2013
On May 13, Abhijeet Rastogi transmitted in part:
> Before wanting to have my answer, I need to understand how a milter
> works. So, it it like when a new mail is in queue, one instance of
> milter script is spawned for each mail?
No. One instance of a user class derived from Milter.Base is created
for each SMTP connection to the MTA. Each connection also has its
own Thread.
> I need to have a functionality like having a python object common to
> all milter instances. The reason I want to do this is because I need a
> persistent database connection to postgres to do some queries and
> fetch data.
So create a global object. Caveat: since each SMTP connection is its own
thread, you need to wrap non-reentrant global objects with something
like:
import thread
my_lock = thread.allocate_lock()
my_global = MakeMyGlobalObject()
class MyMilter(Milter.Base):
# callback picked at random
def hello(name):
my_lock.acquire()
my_global.frobigate(name)
my_lock.release()
...
Another approach is to use the multiprocessing or threading module as
illustrated in the example milter:
http://pythonhosted.org/pymilter/milter-template_8py-example.html
More information about the Pymilter
mailing list