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')
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)
49 class robot_forking(Robot):
50 subproc = 'urllib2' # Default subprocess
52 def check_url(self, bookmark):
57 subproc = self.subproc
59 for attr in dir(self):
60 if attr.startswith('subproc_'):
61 subproc_attrs.append((attr[len('subproc_'):], getattr(self, attr)))
63 subproc += ':' + ':'.join(['='.join((k,v)) for k,v in subproc_attrs])
64 os.environ['BKMK_ROBOT'] = subproc
67 restart_subp(self.log) # Not restart, just start afresh
70 save_parent = bookmark.parent
71 bookmark.parent = None
72 subp_pipe.write_record(pickle.dumps(bookmark))
74 if check_subp.waitForPendingChar(60): # wait a minute
75 new_b = pickle.loads(subp_pipe.read_record())
78 "moved", "size", "md5", "real_title",
79 "last_tested", "last_modified", "test_time",
82 if hasattr(new_b, attr):
83 setattr(bookmark, attr, getattr(new_b, attr))
85 bookmark.error = "Subprocess connection timed out"
86 restart_subp(self.log)
88 bookmark.parent = save_parent
91 error = check_subp.readPendingErrLine()
94 sys.stderr.write("(subp) " + error)
97 except KeyboardInterrupt:
104 stop_subp(None) # Stop subprocess; do not log restarting