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']
17 import cPickle as pickle
21 from subproc import Subprocess, RecordFile
22 from bkmk_objects import Robot
25 # This is to catch 'close failed: [Errno 9] Bad file descriptor' message
26 # from os.close() in Subprocess.die() and errors from the subprocess.
27 sys.stderr = open("err.log", 'at')
34 global check_subp, subp_pipe
36 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"
46 % os.path.dirname(sys.argv[0]),
48 subp_pipe = RecordFile(check_subp)
54 class robot_forking(Robot):
55 subproc = 'urllib' # Default subprocess
57 def check_url(self, bookmark):
62 subproc = self.subproc
64 for attr in dir(self):
65 if attr.startswith('subproc_'):
67 (attr[len('subproc_'):], getattr(self, attr))
70 subproc += ':' + ':'.join(
71 ['='.join((k, v)) for k, v in subproc_attrs]
73 os.environ['BKMK_ROBOT'] = subproc
76 restart_subp(self.log) # Not restart, just start afresh
79 save_parent = bookmark.parent
80 bookmark.parent = None
81 subp_pipe.write_record(pickle.dumps(bookmark))
83 if check_subp.waitForPendingChar(60): # wait a minute
84 new_b = pickle.loads(subp_pipe.read_record())
87 "moved", "size", "md5", "real_title",
88 "last_tested", "last_modified", "test_time",
89 "icon", "icon_href", "charset",
91 if hasattr(new_b, attr):
92 setattr(bookmark, attr, getattr(new_b, attr))
94 bookmark.error = "Subprocess connection timed out"
95 restart_subp(self.log)
97 bookmark.parent = save_parent
100 error = check_subp.readPendingErrLine()
103 sys.stderr.write("(subp) %s" % error)
106 except KeyboardInterrupt:
113 stop_subp(None) # Stop subprocess; do not log restarting