Commit c0a95564 authored by Jim Fulton's avatar Jim Fulton

initial

parent 62e769a2
"""Simple Event Manager Based on Pipes
"""
from ZServer.medusa.select_trigger import trigger
Wakeup=trigger().pull_trigger
"""ZServer output pipe
"""
import thread
from ZEvent import Wakeup
from string import join
TupleType=type(())
class OutputPipe:
def __init__(self):
lock=thread.allocate_lock()
self._r=lock.release
self._a=lock.acquite
self._buf=[]
def write(self, text):
self._a()
try:
self._buf.append(text)
finally: self._r()
Wakeup()
def close(self):
self._a()
try:
b=self._buf
if b: self._buf=tuple(b)
else: self._buf=None
finally: self._r()
def read(self):
"""Read data from the pipe
Return None if the pipe is open but has no data.
Return an empty string if the pipe is closed.
"""
self._a()
try:
b=self._buf
if b is None: return ''
if not b: return None
if type(b) is TupleType: self._buf=None
else: self._buf=[]
return join(b,'')
finally: self._r()
import thread
from ZOutPipe import OutputPipe
class ZRendevous:
def __init__(self, n=1, name='Main'):
sync=thread.allocate_lock()
self._a=sync.acquire
self._r=sync.release
pool=[]
self._lists=pool, [], []
self._a()
try:
while n > 0:
l=thread.allocate_lock()
l.acquire()
pool.append(l)
thread.start_new_thread(ZServerPublisher,
(name, self.accept,))
n=n-1
finally: self._r()
def accept(self):
self._a()
try:
pool, requests, ready = self._lists
while not requests:
l=pool[-1]
del pool[-1]
ready.append(l)
self._r()
l.acquire()
self._a()
pool.append(l)
r=self.requests[0]
del self.requets[0]
return r
finally: self._r()
def handle(self, environ, input):
output=OutputPipe()
self._a()
try:
pool, requests, ready = self._lists
requests.append((input, output, environ))
if ready:
l=ready[-1]
del ready[-1]
l.release()
return output
finally: self._r()
from ZPublisher import publish_module
from cStringIO import StringIO
class ZServerPublisher:
def __init__(self, name, accept):
while 1:
input, output, environ = accept()
try:
publish_module(
name,
stdin=input,
stdout=output,
stderr=StringIO(), # whatever ;)
environ=environ)
finally:
output.close()
import ZRendezvous
handle=ZRendezvous.ZRendevous().handle
"""Simple Event Manager Based on Pipes
"""
from ZServer.medusa.select_trigger import trigger
Wakeup=trigger().pull_trigger
"""ZServer output pipe
"""
import thread
from ZEvent import Wakeup
from string import join
TupleType=type(())
class OutputPipe:
def __init__(self):
lock=thread.allocate_lock()
self._r=lock.release
self._a=lock.acquite
self._buf=[]
def write(self, text):
self._a()
try:
self._buf.append(text)
finally: self._r()
Wakeup()
def close(self):
self._a()
try:
b=self._buf
if b: self._buf=tuple(b)
else: self._buf=None
finally: self._r()
def read(self):
"""Read data from the pipe
Return None if the pipe is open but has no data.
Return an empty string if the pipe is closed.
"""
self._a()
try:
b=self._buf
if b is None: return ''
if not b: return None
if type(b) is TupleType: self._buf=None
else: self._buf=[]
return join(b,'')
finally: self._r()
import thread
from ZOutPipe import OutputPipe
class ZRendevous:
def __init__(self, n=1, name='Main'):
sync=thread.allocate_lock()
self._a=sync.acquire
self._r=sync.release
pool=[]
self._lists=pool, [], []
self._a()
try:
while n > 0:
l=thread.allocate_lock()
l.acquire()
pool.append(l)
thread.start_new_thread(ZServerPublisher,
(name, self.accept,))
n=n-1
finally: self._r()
def accept(self):
self._a()
try:
pool, requests, ready = self._lists
while not requests:
l=pool[-1]
del pool[-1]
ready.append(l)
self._r()
l.acquire()
self._a()
pool.append(l)
r=self.requests[0]
del self.requets[0]
return r
finally: self._r()
def handle(self, environ, input):
output=OutputPipe()
self._a()
try:
pool, requests, ready = self._lists
requests.append((input, output, environ))
if ready:
l=ready[-1]
del ready[-1]
l.release()
return output
finally: self._r()
from ZPublisher import publish_module
from cStringIO import StringIO
class ZServerPublisher:
def __init__(self, name, accept):
while 1:
input, output, environ = accept()
try:
publish_module(
name,
stdin=input,
stdout=output,
stderr=StringIO(), # whatever ;)
environ=environ)
finally:
output.close()
import ZRendezvous
handle=ZRendezvous.ZRendevous().handle
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment