3 This file is a part of Bookmarks database and Internet robot.
6 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 2000-2012 PhiloSoft Design"
8 __license__ = "GNU GPL"
10 __all__ = ['robot_forking']
16 import cPickle as pickle
20 from subproc import Subprocess, RecordFile
21 from bkmk_objects import Robot
24 # This is to catch 'close failed: [Errno 9] Bad file descriptor' message
25 # from os.close() in Subprocess.die() and errors from the subprocess.
26 sys.stderr = open("err.log", 'a')
32 global check_subp, subp_pipe
34 if log: log(" restarting hanging subprocess")
38 def restart_subp(log):
39 global check_subp, subp_pipe
42 check_subp = Subprocess("%s/Robots/bkmk_rforking_sub.py" % os.path.dirname(sys.argv[0]),
44 subp_pipe = RecordFile(check_subp)
47 class robot_forking(Robot):
48 def check_url(self, bookmark):
50 restart_subp(self.log) # Not restart, just start afresh
53 save_parent = bookmark.parent
54 bookmark.parent = None
55 subp_pipe.write_record(pickle.dumps(bookmark))
57 if check_subp.waitForPendingChar(60): # wait a minute
58 new_b = pickle.loads(subp_pipe.read_record())
59 for attr in ("error", "no_error",
60 "moved", "size", "md5", "real_title",
61 "last_tested", "last_modified", "test_time",
63 if hasattr(new_b, attr):
64 setattr(bookmark, attr, getattr(new_b, attr))
66 bookmark.error = "Subprocess connection timed out"
67 restart_subp(self.log)
69 bookmark.parent = save_parent
72 error = check_subp.readPendingErrLine()
75 sys.stderr.write("(subp) " + error)
78 except KeyboardInterrupt:
85 stop_subp(None) # Stop subprocess; do not log restarting