3 This file is a part of Bookmarks database and Internet robot.
6 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 2000-2023 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')
33 global check_subp, subp_pipe
35 if log: log(" restarting hanging subprocess")
40 def restart_subp(log):
41 global check_subp, subp_pipe
44 check_subp = Subprocess("%s/Robots/bkmk_rforking_sub.py" % os.path.dirname(sys.argv[0]),
46 subp_pipe = RecordFile(check_subp)
52 class robot_forking(Robot):
53 subproc = 'urllib2' # Default subprocess
55 def check_url(self, bookmark):
60 subproc = self.subproc
62 for attr in dir(self):
63 if attr.startswith('subproc_'):
64 subproc_attrs.append((attr[len('subproc_'):], getattr(self, attr)))
66 subproc += ':' + ':'.join(
67 ['='.join((k, v)) for k, v in subproc_attrs]
69 os.environ['BKMK_ROBOT'] = subproc
72 restart_subp(self.log) # Not restart, just start afresh
75 save_parent = bookmark.parent
76 bookmark.parent = None
77 subp_pipe.write_record(pickle.dumps(bookmark))
79 if check_subp.waitForPendingChar(60): # wait a minute
80 new_b = pickle.loads(subp_pipe.read_record())
83 "moved", "size", "md5", "real_title",
84 "last_tested", "last_modified", "test_time",
87 if hasattr(new_b, attr):
88 setattr(bookmark, attr, getattr(new_b, attr))
90 bookmark.error = "Subprocess connection timed out"
91 restart_subp(self.log)
93 bookmark.parent = save_parent
96 error = check_subp.readPendingErrLine()
99 sys.stderr.write("(subp) " + error)
102 except KeyboardInterrupt:
109 stop_subp(None) # Stop subprocess; do not log restarting