Source (Text):
Threads inside of Zope
from Acquisition import Implicit
from threading import Thread
class ReadOnlyUglyZopeThread(Implicit, Thread):
""" This is doable but you need to know the limitations """
_Thread_init_ = Thread.__init__
def __init__(self):
_Thread_init_()
def run(self):
print self.Control_Panel.getId()
get_transaction().commit()
def extROThread(self):
_thread=ReadOnlyUglyZopeThread().__of__(self)
_thread.run()
from threading import Thread
from ZPublisher.HTTPRequest import HTTPRequest
from ZPublisher.HTTPResponse import HTTPResponse
from ZPublisher.BaseRequest import RequestContainer
class ZopeThread(Thread):
""" Best practice - dont inherient from Acquisition
Manage connections and transactions yourself.
Blantantly rip off of code Kapil Thangavelu sent me.
"""
def __init__(self):
Thread.__init__(self)
def run(self):
try:
from Zope import DB
conn = DB.open()
root = conn.root()
app = root['Application']
ctx = self.getContext(app)
print ctx.Control_Panel.getId()
except:
import sys
fh = open(error_file, 'a')
ec, e, tb = sys.exc_info()
print >> fh, "%s An Error Occured"%(str(DateTime()))
print >> fh, "%s: %s"%(str(ec), str(e))
traceback.print_tb(tb, file=fh)
fh.flush()
fh.close()
try:
get_transaction().abort()
conn.close()
except:
pass
def getContext(self, app):
resp = HTTPResponse(stdout=None)
env = {
'SERVER_NAME':'localhost',
'SERVER_PORT':'8080',
'REQUEST_METHOD':'GET'
}
req = HTTPRequest(None, env, resp)
return app.__of__(RequestContainer(REQUEST = req))
def ext_Thread(self):
_thread=ZopeThread()
_thread.run()
def process_reports(app):
import Reports
obj=nextReport()
app._p_jar.sync()
klass = getattr(Reports, obj.kwargs['typeOfReport'])
try:
context=app.unrestrictedTraverse(obj.kwargs['path'])
get_transaction().begin()
report = klass(*obj.args, **obj.kwargs).__of__(context)
pdf = ReportGen().createReport(report())
except:
filename = obj.kwargs['filename']
t,v,tb = sys.exc_info()
formatted_tb = traceback.format_tb(tb)
formatted = traceback.format_exception_only(t,v)
error_msg = formatted[-1]
error_file = open(filename[:-4]+'.txt','w')
error_file.write(error_msg)
error_file.write('\n\n')
error_file.writelines(formatted_tb)
error_file.write('\n%s\n' % str(obj.__dict__))
error_file.close()
def begin():
storage=ZEO.ClientStorage.ClientStorage((host, port), name='Report Server')
db=DB(storage)
connection=db.open()
root=connection.root()
app=root['Application']
ctx=getContext(app)
process_reports(ctx)
if __name__=='__main__':
begin()
|