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