python - TypeError: can't pickle file objects webpy and subprocess -


i trying make following code possible. keep getting "typeerror: can't pickle file objects" error. both new python , webpy. can tell how can achieve code.

import web  import subprocess  web.config.debug = false urls = (     "/start", "start",    "/end", "end" ) app = web.application(urls, locals()) s = web.session.session(app, web.session.diskstore('sessions'), initializer={"p": none})  class start:     def get(self):         s.p = subprocess.popen(['ls', '-a'], stdout=subprocess.pipe, stderr=subprocess.pipe)         return ""  class end:     def get(self):         out, err = s.p.communicate();         return out   if __name__ == "__main__":     app.run() 

the error keep seeing python27/lib/python2.7/copy_reg.py", line 70, in _reduce_ex raise typeerror, "can't pickle %s objects" % base.name typeerror: can't pickle file objects

i am, myself, webpy newbie. nevertheless, after doing "research", seems webpy cannot pickle subprocess.popen object[1]. so, lets try following approach -- is, creating in end response , printing output.

in other words:

import web  import subprocess  web.config.debug = false urls = (     "/start", "start",    "/end", "end" ) app = web.application(urls, locals()) s = web.session.session(         app, web.session.diskstore('sessions'), initializer={"p": none})  class start:     def get(self):         s.p = ['ls', '-a']         return ""  class end:     def get(self):         out, err = subprocess.popen(                        s.p , stdout=subprocess.pipe,                        stderr=subprocess.pipe).communicate()         s.kill() # kill session.         return out   if __name__ == "__main__":     app.run() 

[1] can see:

import pickle import subprocess  open('bla', 'wb') f:     pickle.dump(subprocess.popen(['ls'],                 stdout=subprocess.pipe,                 stderr=subprocess.pipe), f) 

the subprocess.popen returns file descriptor , traceback infer pickle cannot serialize file descriptors.

traceback (most recent call last):   file "<stdin>", line 2, in <module>   file "/usr/lib64/python2.7/pickle.py", line 1370, in dump     pickler(file, protocol).dump(obj)   file "/usr/lib64/python2.7/pickle.py", line 224, in dump     self.save(obj)   file "/usr/lib64/python2.7/pickle.py", line 331, in save     self.save_reduce(obj=obj, *rv)   file "/usr/lib64/python2.7/pickle.py", line 419, in save_reduce     save(state)   file "/usr/lib64/python2.7/pickle.py", line 286, in save     f(self, obj) # call unbound method explicit self   file "/usr/lib64/python2.7/pickle.py", line 649, in save_dict     self._batch_setitems(obj.iteritems())   file "/usr/lib64/python2.7/pickle.py", line 663, in _batch_setitems     save(v)   file "/usr/lib64/python2.7/pickle.py", line 306, in save     rv = reduce(self.proto)   file "/usr/lib64/python2.7/copy_reg.py", line 70, in _reduce_ex     raise typeerror, "can't pickle %s objects" % base.__name__ typeerror: can't pickle file objects 

in addition that, pickle documentation mentions supports only booleans, integers, floating point numbers, complex numbers, strings, bytes objects, byte arrays, , none. thus, seems hopeless.

edit

if insist on creating process' file-descriptor, when receiving start request client, can use dictionary map session file-descriptor.

it should this:

s = web.session.session(         app, web.session.diskstore('sessions'), initializer={"p": none}) session_map = {}  class start:     def get(self):         session_map[s] = subprocess.popen(['ls', '-a'],                                           stdout=subprocess.pipe,                                           stderr=subprocess.pipe)         return ""  class end:     def get(self):         out, err = session_process[s].communicate()         session_process.pop(s, none)         s.kill         return out 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -