June 16th, 2004, 04:11 PM
-
Problems Pickling after making a program a Daemon
hi all,
Well i was writing a program which i needed to be a daemon. Iwas pickling objects inot and out of a file in that program. After I have made that program a daemon by the two fork method, the pickle doesnt seem to work. I write to a file (as it is a daemon,i dont "echo")before and after the pickle command,only the line before the pickle gets written. Have any of you had this probelm.It doesnt give any errors.
I have pasted the code also,and the file to read from.Please help me.
***************************
Code:
import os
import sys
def read(objlist):
flog = file("logfile","a")
try:
fp = file('newone','r')
except IOError:
flog.write('hi In read Couldn\'t open %s for reading' %('newone'))
sys.exit()
else:
flog.write('new one opened in read mode\n')
i=0
flag=0
templist = []
objlist = cPickle.load(fp)
flog.write('About to picle the items out')
flog.write(str(len(objlist)))
flog.close()
def again_daemon():
try:
pid = os.fork()
if pid > 0:
#---first parent
sys.exit(0)
except OSError,e:
print >>sys.stderr, "fork #1 failed %d (%s)" % (e.errno,e.strerror)
sys.exit(1)
#---make a clean process---
os.chdir('/')
os.setsid()
os.umask(000)
child_in =open('/dev/null','r')
child_out=open('/dev/null','w')
os.dup2(child_in.fileno(), sys.stdin.fileno())
os.dup2(child_out.fileno(), sys.stdout.fileno())
os.dup2(child_out.fileno(), sys.stderr.fileno())
#---second fork---
try:
pid = os.fork()
if pid > 0:
#---second parent---
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #2 failed %d (%s)" % (e.errno,e.strerror)
sys.exit(1)
return (0)
if __name__ == "__main__":
objlist =[]
retcode = again_daemon()
read(objlist)
Here is the file 'newone' to read from.
(lp1
(i__main__
entry
p2
(dp3
S'remark'
p4
S'none'
sS'title'
p5
S'Meet Devshed.'
sS'day'
p6
I16
sS'time'
p7
S'9 am'
Last edited by netytan; June 18th, 2004 at 04:43 AM.
Reason: Adding code tags get again.
June 17th, 2004, 04:18 AM
-
Please use a code tags and repost your question. Someone might be able to help.
Last edited by Grim Archon; June 17th, 2004 at 04:23 AM.
June 17th, 2004, 01:30 PM
-
hi had pasted the code with spacing corretcly.But dont know why it came without indentation.
June 17th, 2004, 05:32 PM
-
When you post code there are some code tags that make the code easier fore people to read. Look at the buttons right above the text field where you post and you'll find it.
June 18th, 2004, 02:18 AM
-
There is a Sticky message at the top of the forum that might be useful
June 18th, 2004, 02:56 AM
-
Click the "#" wrap (code) icon and paste your codes
June 18th, 2004, 04:45 AM
-
I've done this for you this time but in future could you please used them, this tread explains how to ask a question and the importance of using code tags with Python.
It also looks like you have an indentation error on the first line in read(). You seem to be using several different indentation levels all over the place i.e. some lines use 2 spaces and others 4 etc.
four spaces is the standard indentation level so i would probably use that.
Can i also sugest that you choose to use either file() or open() and not both for consistancy reasons
. Also i would probably replace
Code:
print >> sys.stderr, 'error message'
sys.exit(1)
with
Code:
raise SystemExit, 'error message'
which is equivelent to calling sys.exit('error message') but makes more sence to me at least. Just a few sugestions to get you stated
.
Later,
Mark.
Last edited by netytan; June 18th, 2004 at 05:27 AM.