#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    2
    Rep Power
    0

    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.
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    2
    Rep Power
    0
    hi had pasted the code with spacing corretcly.But dont know why it came without indentation.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    266
    Rep Power
    13
    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.
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    There is a Sticky message at the top of the forum that might be useful
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    45
    Rep Power
    10
    Click the "#" wrap (code) icon and paste your codes
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo