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", 'a')
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" % os.path.dirname(sys.argv[0]),
47 subp_pipe = RecordFile(check_subp)
53 class robot_forking(Robot):
54 subproc = 'urllib2' # Default subprocess
56 def check_url(self, bookmark):
61 subproc = self.subproc
63 for attr in dir(self):
64 if attr.startswith('subproc_'):
65 subproc_attrs.append((attr[len('subproc_'):], getattr(self, attr)))
67 subproc += ':' + ':'.join(
68 ['='.join((k, v)) for k, v in subproc_attrs]
70 os.environ['BKMK_ROBOT'] = subproc
73 restart_subp(self.log) # Not restart, just start afresh
76 save_parent = bookmark.parent
77 bookmark.parent = None
78 subp_pipe.write_record(pickle.dumps(bookmark))
80 if check_subp.waitForPendingChar(60): # wait a minute
81 new_b = pickle.loads(subp_pipe.read_record())
84 "moved", "size", "md5", "real_title",
85 "last_tested", "last_modified", "test_time",
88 if hasattr(new_b, attr):
89 setattr(bookmark, attr, getattr(new_b, attr))
91 bookmark.error = "Subprocess connection timed out"
92 restart_subp(self.log)
94 bookmark.parent = save_parent
97 error = check_subp.readPendingErrLine()
100 sys.stderr.write("(subp) " + error)
103 except KeyboardInterrupt:
110 stop_subp(None) # Stop subprocess; do not log restarting