Browse Source

add multithread processes lock (for the crontab tasks compatibility) #1

main
D4708 3 years ago
parent
commit
add9da3c94
  1. 28
      follower.py

28
follower.py

@ -13,7 +13,11 @@ import sys, cPickle @@ -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,25 +31,30 @@ if len(sys.argv) > 1: @@ -27,25 +31,30 @@ 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:
print "blockchain reading..."
db.dataLock = True
cPickle.dump(db, open(dbFileName, "w"))
while True:
print "blockchain reading..."
while True:
block = twister.getblock(nextHash)
db.lastBlockHash = block["hash"]
blocksInStep = blocksInStep - 1
if blocksInStep < 0:
db.dataLock = False
break
print "read block", str(block["height"])# + "\r",
@ -58,6 +67,9 @@ while True: @@ -58,6 +67,9 @@ while True:
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."

Loading…
Cancel
Save