[Twisted-Python] Simple app pickle issue when stopping
Still trying to learn/understand twisted. Mind exploding/getting
frustrated. I created this very simple app:
#!/usr/bin/python
from twisted.application import internet, service from twisted.web import
resource, server
class SimpleResource(resource.Resource):
def __init__(self):
resource.Resource.__init__(self)
self.isLeaf=1
def render_GET(self, request):
return 'simple'
application = service.Application('server') serviceCollection =
service.IServiceCollection(application)
internet.TCPServer(8080,server.Site(SimpleResource())).setServiceParent(serv
iceCollection)
Then I run "twistd -y scratch.py" and it starts up correctly. Great.
I can go to http://localhost:8080 and see the web page "simple". Wonderful.
I hit control^C to stop the server and I get pickling errors. Not good.
Call stack listed below:
IF I go into the python interactive environment and import sratch (and
pickle), I can instantiate SimpleResrouce and pickle it with no problem.
Help!
Alex
Call stack:
C:\Documents and Settings\Alexander May\My
Documents\maygold-dev\central-server>twistd -y scratch.py
2004/05/15 14:51 Eastern Daylight Time [-] Log opened.
2004/05/15 14:51 Eastern Daylight Time [-] twistd 1.2.0
(C:\Python23\python.exe 2.3.3) starting up
2004/05/15 14:51 Eastern Daylight Time [-] reactor class:
twisted.internet.default.SelectReactor
2004/05/15 14:51 Eastern Daylight Time [-] twisted.web.server.Site starting
on 8080
2004/05/15 14:51 Eastern Daylight Time [-] Starting factory
On Sat, 15 May 2004 15:14:49 -0400, "Alexander May"
Still trying to learn/understand twisted. Mind exploding/getting frustrated. I created this very simple app: [snip]
Instances are pickled by saving the name of their class and their associated state. Since the SimpleResource class isn't defined in a real module, instances of it cannot be pickled (its name is a lie). Move the class definition into a real module, then import it from your tac file. The pickle problems should go away. Also, if pickling isn't important, you can just pass the -o flag to twistd and the application will not be saved on shutdown. Jp
Thank you. Soon, I'll be attaching to a database, and the pickling stuff
won't matter, so I'll use the -o flag as you suggest.
Anyone have any experience with gadfly and using it with twisted? I'm
prototyping so I'm tempted to give it a try and avoid setting up a real
database (not that gadfly isn't real). And while I'm off subject, anyone
have a suggestion for integrating authorization with windows (and probably
Linux down the road)? I'm reading the docs...
Thanks,
Alex
-----Original Message-----
From: twisted-python-bounces@twistedmatrix.com
[mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of
exarkun@divmod.com
Sent: Saturday, May 15, 2004 4:34 PM
To: Twisted discussion stuff
Subject: Re: [Twisted-Python] Simple app pickle issue when stopping
On Sat, 15 May 2004 15:14:49 -0400, "Alexander May"
Still trying to learn/understand twisted. Mind exploding/getting frustrated. I created this very simple app: [snip]
Instances are pickled by saving the name of their class and their associated state. Since the SimpleResource class isn't defined in a real module, instances of it cannot be pickled (its name is a lie). Move the class definition into a real module, then import it from your tac file. The pickle problems should go away. Also, if pickling isn't important, you can just pass the -o flag to twistd and the application will not be saved on shutdown. Jp _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Sat, 2004-05-15 at 15:14, Alexander May wrote:
Still trying to learn/understand twisted. Mind exploding/getting frustrated. I created this very simple app:
--snip--
File "C:\Python23\Lib\site-packages\twisted\persisted\sob.py", line 119, in dumpFunc pickle.dump(obj, file, 1) cPickle.PicklingError: Can't pickle __builtin__.SimpleResource: attribute lookup __builtin__.SimpleResource faied
Try defining SimpleResource inside your own module, instead of in scratch.py. That way, when it goes to pickle your resource, it looks up 'mymodule.SimpleResource' instead of '__builtin__.SimpleResource'. Worth a try? Separation of application code and deployment code is pretty important for Twisted apps. The idea is that .tac and .rpy files (and other such things) shouldn't have your actual application in them - they should just be glue to get your application to work properly within a given system. -- Alex Levy WWW: http://mesozoic.geecs.org/ "Never let your sense of morals prevent you from doing what is right." -- Salvor Hardin, Isaac Asimov's _Foundation_
Yes, that worked. Thank you. -----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python-bounces@twistedmatrix.com] On Behalf Of Alex Levy Sent: Saturday, May 15, 2004 4:40 PM To: Twisted discussion stuff Subject: [Twisted-Python] Re: Simple app pickle issue when stopping On Sat, 2004-05-15 at 15:14, Alexander May wrote:
Still trying to learn/understand twisted. Mind exploding/getting frustrated. I created this very simple app:
--snip--
File "C:\Python23\Lib\site-packages\twisted\persisted\sob.py", line 119, in dumpFunc pickle.dump(obj, file, 1) cPickle.PicklingError: Can't pickle __builtin__.SimpleResource: attribute lookup __builtin__.SimpleResource faied
Try defining SimpleResource inside your own module, instead of in scratch.py. That way, when it goes to pickle your resource, it looks up 'mymodule.SimpleResource' instead of '__builtin__.SimpleResource'. Worth a try? Separation of application code and deployment code is pretty important for Twisted apps. The idea is that .tac and .rpy files (and other such things) shouldn't have your actual application in them - they should just be glue to get your application to work properly within a given system. -- Alex Levy WWW: http://mesozoic.geecs.org/ "Never let your sense of morals prevent you from doing what is right." -- Salvor Hardin, Isaac Asimov's _Foundation_
participants (3)
-
Alex Levy
-
Alexander May
-
exarkun@divmod.com