Commit 2224b1aa authored by Jeremy Hylton's avatar Jeremy Hylton

Add run_server() method and profile support from ZEO2

parent a2b3f6f6
"""Library for forking storage server and connecting client storage"""
import asyncore
import atexit
import os
import profile
import sys
import time
import types
import ThreadedAsync
import ZEO.ClientStorage, ZEO.StorageServer
PROFILE = 0
class ZEOServerExit(asyncore.file_dispatcher):
"""Used to exit ZEO.StorageServer when run is done"""
......@@ -36,24 +42,30 @@ def start_zeo_server(storage, addr):
rd, wr = os.pipe()
pid = os.fork()
if pid == 0:
# in the child, run the storage server
try:
os.close(wr)
ZEOServerExit(rd)
serv = ZEO.StorageServer.StorageServer(addr, {'1':storage})
asyncore.loop()
storage.close()
if domain == "AF_UNIX":
os.unlink(addr)
if cleanup:
cleanup()
finally:
os._exit(0)
if PROFILE:
p = profile.Profile()
p.runctx("run_server(storage, addr, rd, wr)", globals(),
locals())
p.dump_stats("stats.s.%d" % os.getpid())
else:
run_server(storage, addr, rd, wr)
os._exit(0)
else:
os.close(rd)
return pid, ZEOClientExit(wr)
def start_zeo(storage, cache=None, cleanup=None, domain="AF_INET"):
def run_server(storage, addr, rd, wr):
# in the child, run the storage server
os.close(wr)
ZEOServerExit(rd)
serv = ZEO.StorageServer.StorageServer(addr, {'1':storage})
asyncore.loop()
storage.close()
if isinstance(addr, types.StringType):
os.unlink(addr)
def start_zeo(storage, cache=None, cleanup=None, domain="AF_INET",
storage_id="1"):
"""Setup ZEO client-server for storage.
Returns a ClientStorage instance and a ZEOClientExit instance.
......@@ -71,6 +83,10 @@ def start_zeo(storage, cache=None, cleanup=None, domain="AF_INET"):
raise ValueError, "bad domain: %s" % domain
pid, exit = start_zeo_server(storage, addr)
s = ZEO.ClientStorage.ClientStorage(addr, debug=1, client=cache)
s = ZEO.ClientStorage.ClientStorage(addr, storage_id,
debug=1, client=cache)
if hasattr(s, 'is_connected'):
while not s.is_connected():
time.sleep(0.1)
return s, exit, pid
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