]> git.phdru.name Git - bookmarks_db.git/blob - Robots/bkmk_rforking.py
Version 3.3.1.
[bookmarks_db.git] / Robots / bkmk_rforking.py
1 """
2    Forking robot
3
4    Written by BroytMann, Mar 2000 - Jun 2002. Copyright (C) 2000-2002 PhiloSoft Design
5 """
6
7
8 try:
9    import cPickle
10    pickle = cPickle
11 except ImportError:
12    import pickle
13
14 import sys, os
15 from subproc import Subprocess, RecordFile
16
17
18 check_subp = None
19 subp_pipe = None
20
21 def stop_subp(log):
22    global check_subp, subp_pipe
23    if check_subp:
24       if log: log("   restarting hanging subprocess")
25       del check_subp
26    del subp_pipe
27
28 def restart_subp(log):
29    global check_subp, subp_pipe
30    stop_subp(log)
31
32    check_subp = Subprocess("%s/Robots/bkmk_rforking_sub.py" % os.path.dirname(sys.argv[0]))
33    subp_pipe = RecordFile(check_subp)
34
35
36 from bkmk_objects import Robot
37
38 class robot_forking(Robot):
39    def check_url(self, bookmark, url_type, url_rest):
40       if not check_subp:
41          restart_subp(self.log) # Not restart, just start afresh
42
43       try:
44          save_parent = bookmark.parent
45          bookmark.parent = None
46
47          bookmark.tempfname = self.tempfname
48          subp_pipe.write_record(pickle.dumps(bookmark))
49
50          if check_subp.waitForPendingChar(900): # wait 15 minutes
51             new_b = pickle.loads(subp_pipe.read_record())
52             for attr in ("error", "no_error",
53                   "moved", "size", "md5", "real_title",
54                   "last_tested", "last_modified", "test_time"):
55                if hasattr(new_b, attr):
56                   setattr(bookmark, attr, getattr(new_b, attr))
57          else:
58             bookmark.error = "Subprocess connection timed out"
59             restart_subp(self.log)
60
61          bookmark.parent = save_parent
62
63       except KeyboardInterrupt:
64          return 0
65
66       # Tested
67       return 1
68
69
70    def stop(self):
71       stop_subp(None) # Stop subprocess; do not log restarting