3 This file is a part of Bookmarks database and Internet robot.
6 __version__ = "$Revision$"[11:-2]
7 __revision__ = "$Id$"[5:-2]
8 __date__ = "$Date$"[7:-2]
9 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 2000-2011 PhiloSoft Design"
11 __license__ = "GNU GPL"
13 __all__ = ['robot_forking']
19 import cPickle as pickle
23 from subproc import Subprocess, RecordFile
24 from bkmk_objects import Robot
27 # This is to catch 'close failed: [Errno 9] Bad file descriptor' message
28 # from os.close() in Subprocess.die() and errors from the subprocess.
29 sys.stderr = open("err.log", 'a')
35 global check_subp, subp_pipe
37 if log: log(" restarting hanging subprocess")
41 def restart_subp(log):
42 global check_subp, subp_pipe
45 check_subp = Subprocess("%s/Robots/bkmk_rforking_sub.py" % os.path.dirname(sys.argv[0]),
47 subp_pipe = RecordFile(check_subp)
50 class robot_forking(Robot):
51 def check_url(self, bookmark):
53 restart_subp(self.log) # Not restart, just start afresh
56 save_parent = bookmark.parent
57 bookmark.parent = None
58 subp_pipe.write_record(pickle.dumps(bookmark))
60 if check_subp.waitForPendingChar(60): # wait a minute
61 new_b = pickle.loads(subp_pipe.read_record())
62 for attr in ("error", "no_error",
63 "moved", "size", "md5", "real_title",
64 "last_tested", "last_modified", "test_time",
66 if hasattr(new_b, attr):
67 setattr(bookmark, attr, getattr(new_b, attr))
69 bookmark.error = "Subprocess connection timed out"
70 restart_subp(self.log)
72 bookmark.parent = save_parent
75 error = check_subp.readPendingErrLine()
78 sys.stderr.write("(subp) " + error)
81 except KeyboardInterrupt:
88 stop_subp(None) # Stop subprocess; do not log restarting