From add9da3c9402c125b9912d040b8df6e9ec72d349 Mon Sep 17 00:00:00 2001 From: D4708 Date: Sat, 26 Jun 2021 18:57:53 +0300 Subject: [PATCH] add multithread processes lock (for the crontab tasks compatibility) #1 --- follower.py | 54 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/follower.py b/follower.py index e9f6b81..ec41ff6 100644 --- a/follower.py +++ b/follower.py @@ -13,7 +13,11 @@ import sys, cPickle dbFileName = "twisterDataGuard.pickle" nodeUserName = "twisterdataguard" -blocksInStep = 10000 +blocksInStep = 10 + +class MyDb: + lastBlockHash = 0 + dataLock = False try: from bitcoinrpc.authproxy import AuthServiceProxy @@ -27,37 +31,45 @@ if len(sys.argv) > 1: twister = AuthServiceProxy(serverUrl) -class MyDb: - lastBlockHash = 0 - try: db = cPickle.load(open(dbFileName)) nextHash = db.lastBlockHash + dataLock = db.dataLock except: db = MyDb() nextHash = twister.getblockhash(0) + dataLock = db.dataLock + +if not dataLock: + + db.dataLock = True + cPickle.dump(db, open(dbFileName, "w")) + + print "blockchain reading..." -print "blockchain reading..." + while True: -while True: + block = twister.getblock(nextHash) + db.lastBlockHash = block["hash"] - block = twister.getblock(nextHash) - db.lastBlockHash = block["hash"] + blocksInStep = blocksInStep - 1 + if blocksInStep < 0: + db.dataLock = False + break - blocksInStep = blocksInStep - 1 - if blocksInStep < 0: - break + print "read block", str(block["height"])# + "\r", - print "read block", str(block["height"])# + "\r", + for u in block["usernames"]: + print "follow", u + twister.follow(nodeUserName, [u]) + if block.has_key("nextblockhash"): + nextHash = block["nextblockhash"] + else: + break - for u in block["usernames"]: - print "follow", u - twister.follow(nodeUserName, [u]) - if block.has_key("nextblockhash"): - nextHash = block["nextblockhash"] - else: - break + cPickle.dump(db, open(dbFileName, "w")) -cPickle.dump(db,open(dbFileName, "w")) + print "task completed." -print "task completed." +else: + print "operation locked by the running process."