[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 <twisted.web.server.Site instance at 0x00C52B70> 2004/05/15 14:51 Eastern Daylight Time [HTTPChannel,0,127.0.0.1] 127.0.0.1 - - [15/May/2004:18:51:26 +0000] "GET / HTTP 1.1" 200 6 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" 2004/05/15 14:51 Eastern Daylight Time [-] (Port 8080 Closed) 2004/05/15 14:51 Eastern Daylight Time [-] Stopping factory <twisted.web.server.Site instance at 0x00C52B70> 2004/05/15 14:51 Eastern Daylight Time [-] Saving server application to server-shutdown.tap... 2004/05/15 14:51 Eastern Daylight Time [-] Traceback (most recent call last): File "C:\Python23\Lib\site-packages\twisted\internet\defer.py", line 227, in callback self._startRunCallbacks(result) File "C:\Python23\Lib\site-packages\twisted\internet\defer.py", line 288, in _startRunCallbacks self._runCallbacks() File "C:\Python23\Lib\site-packages\twisted\internet\defer.py", line 313, in _runCallbacks self.result = callback(self.result, *args, **kw) File "C:\Python23\Lib\site-packages\twisted\internet\base.py", line 317, in _cbContinueSystemEvent self._continueSystemEvent(eventType) --- <exception caught here> --- File "C:\Python23\Lib\site-packages\twisted\internet\base.py", line 325, in _continueSystemEvent callable(*args, **kw) File "C:\Python23\Lib\site-packages\twisted\persisted\sob.py", line 135, in save self._saveTemp(filename, passphrase, dumpFunc) File "C:\Python23\Lib\site-packages\twisted\persisted\sob.py", line 103, in _saveTemp dumpFunc(self.original, f) 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 2004/05/15 14:51 Eastern Daylight Time [-] Main loop terminated. 2004/05/15 14:51 Eastern Daylight Time [-] Server Shut Down.

On Sat, 15 May 2004 15:14:49 -0400, "Alexander May" <alex-news@comcast.net> wrote:
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" <alex-news@comcast.net> wrote:
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--
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--
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_

On Sat, 15 May 2004 15:14:49 -0400, "Alexander May" <alex-news@comcast.net> wrote:
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" <alex-news@comcast.net> wrote:
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--
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--
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