[Zope-Checkins] CVS: Zope3/lib/python/Zope/Server/Thread - SelectTrigger.py:1.1.2.2 __init__.py:1.1.2.2
Shane Hathaway
shane@cvs.zope.org
Thu, 4 Apr 2002 13:46:27 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/Server/Thread
In directory cvs.zope.org:/tmp/cvs-serv17993/Thread
Modified Files:
Tag: Zope3-Server-Branch
SelectTrigger.py __init__.py
Log Message:
Just fixed line endings. No carriage returns.
=== Zope3/lib/python/Zope/Server/Thread/SelectTrigger.py 1.1.2.1 => 1.1.2.2 ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
-#
+#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
-#
+#
##############################################################################
# -*- Mode: Python; tab-width: 4 -*-
@@ -26,9 +26,9 @@
if os.name == 'posix':
class Trigger(asyncore.file_dispatcher):
-
+
"Wake up a call to select() running in the main thread"
-
+
# This is useful in a context where you are using Medusa's I/O
# subsystem to deliver data, but the data is generated by another
# thread. Normally, if Medusa is in the middle of a call to
@@ -37,7 +37,7 @@
# If the trigger is 'pulled' by another thread, it should immediately
# generate a READ event on the trigger object, which will force the
# select() invocation to return.
-
+
# A common use for this facility: letting Medusa manage I/O for a
# large number of connections; but routing each request through a
# thread chosen from a fixed-size thread pool. When a thread is
@@ -46,7 +46,7 @@
# by Medusa. [picture a server that can process database queries
# rapidly, but doesn't want to tie up threads waiting to send data
# to low-bandwidth connections]
-
+
# The other major feature provided by this class is the ability to
# move work back into the main thread: if you call pull_trigger()
# with a thunk argument, when select() wakes up and receives the
@@ -56,26 +56,26 @@
# why this is true, imagine this scenario: A thread tries to push some
# new data onto a channel's outgoing data queue at the same time that
# the main thread is trying to remove some]
-
+
def __init__ (self):
r, w = os.pipe()
self.trigger = w
asyncore.file_dispatcher.__init__ (self, r)
self.lock = thread.allocate_lock()
self.thunks = []
-
+
def __repr__ (self):
return '<select-trigger (pipe) at %x>' % id(self)
-
+
def readable (self):
return 1
-
+
def writable (self):
return 0
-
+
def handle_connect (self):
pass
-
+
def pull_trigger (self, thunk=None):
# print 'PULL_TRIGGER: ', len(self.thunks)
if thunk:
@@ -85,7 +85,7 @@
finally:
self.lock.release()
os.write (self.trigger, 'x')
-
+
def handle_read (self):
self.recv (8192)
try:
@@ -101,23 +101,23 @@
self.thunks = []
finally:
self.lock.release()
-
+
else:
# win32-safe version
class Trigger (asyncore.dispatcher):
-
+
address = ('127.9.9.9', 19999)
-
+
def __init__ (self):
a = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
w = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
-
+
# set TCP_NODELAY to true to avoid buffering
w.setsockopt(socket.IPPROTO_TCP, 1, 1)
-
+
# tricky: get a pair of connected sockets
host='127.0.0.1'
port=19999
@@ -130,7 +130,7 @@
if port <= 19950:
raise 'Bind Error', 'Cannot bind trigger!'
port=port - 1
-
+
a.listen (1)
w.setblocking (0)
try:
@@ -141,24 +141,24 @@
a.close()
w.setblocking (1)
self.trigger = w
-
+
asyncore.dispatcher.__init__ (self, r)
self.lock = thread.allocate_lock()
self.thunks = []
self._trigger_connected = 0
-
+
def __repr__ (self):
return '<select-trigger (loopback) at %x>' % id(self)
-
+
def readable (self):
return 1
-
+
def writable (self):
return 0
-
+
def handle_connect (self):
pass
-
+
def pull_trigger (self, thunk=None):
if thunk:
try:
@@ -167,7 +167,7 @@
finally:
self.lock.release()
self.trigger.send ('x')
-
+
def handle_read (self):
self.recv (8192)
try:
@@ -181,22 +181,22 @@
self.thunks = []
finally:
self.lock.release()
-
-
+
+
the_trigger = None
class TriggerFile:
"A 'triggered' file object"
-
+
buffer_size = 4096
-
+
def __init__ (self, parent):
global the_trigger
if the_trigger is None:
the_trigger = trigger()
self.parent = parent
self.buffer = ''
-
+
def write (self, data):
self.buffer = self.buffer + data
if len(self.buffer) > self.buffer_size:
@@ -204,10 +204,10 @@
the_trigger.pull_trigger (
lambda d=d,p=self.parent: p.push (d)
)
-
+
def writeline (self, line):
self.write (line+'\r\n')
-
+
def writelines (self, lines):
self.write (
string.joinfields (
@@ -215,33 +215,33 @@
'\r\n'
) + '\r\n'
)
-
+
def flush (self):
if self.buffer:
d, self.buffer = self.buffer, ''
the_trigger.pull_trigger (
lambda p=self.parent,d=d: p.push (d)
)
-
+
def softspace (self, *args):
pass
-
+
def close (self):
# in a derived class, you may want to call trigger_close() instead.
self.flush()
self.parent = None
-
+
def trigger_close (self):
d, self.buffer = self.buffer, ''
p, self.parent = self.parent, None
the_trigger.pull_trigger (
lambda p=p,d=d: (p.push(d), p.close_when_done())
)
-
+
if __name__ == '__main__':
import time
-
+
def thread_function (output_file, i, n):
print 'entering thread_function'
while n:
@@ -251,19 +251,19 @@
n = n - 1
output_file.close()
print 'exiting thread_function'
-
+
class thread_parent (asynchat.async_chat):
-
+
def __init__ (self, conn, addr):
self.addr = addr
asynchat.async_chat.__init__ (self, conn)
self.set_terminator ('\r\n')
self.buffer = ''
self.count = 0
-
+
def collect_incoming_data (self, data):
self.buffer = self.buffer + data
-
+
def found_terminator (self):
data, self.buffer = self.buffer, ''
if not data:
@@ -274,20 +274,20 @@
tf = TriggerFile (self)
self.count = self.count + 1
thread.start_new_thread (thread_function, (tf, self.count, n))
-
+
class ThreadServer(asyncore.dispatcher):
-
+
def __init__ (self, family=socket.AF_INET, address=('', 9003)):
asyncore.dispatcher.__init__ (self)
self.create_socket (family, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind (address)
self.listen (5)
-
+
def handle_accept (self):
conn, addr = self.accept()
tp = thread_parent (conn, addr)
-
+
ThreadServer()
#asyncore.loop(1.0, use_poll=1)
try:
=== Zope3/lib/python/Zope/Server/Thread/__init__.py 1.1.2.1 => 1.1.2.2 ===
-#
-# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""
-
-$Id$
-"""
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+
+$Id$
+"""