Browse Source

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

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

54
follower.py

@ -13,7 +13,11 @@ import sys, cPickle
dbFileName = "twisterDataGuard.pickle" dbFileName = "twisterDataGuard.pickle"
nodeUserName = "twisterdataguard" nodeUserName = "twisterdataguard"
blocksInStep = 10000 blocksInStep = 10
class MyDb:
lastBlockHash = 0
dataLock = False
try: try:
from bitcoinrpc.authproxy import AuthServiceProxy from bitcoinrpc.authproxy import AuthServiceProxy
@ -27,37 +31,45 @@ if len(sys.argv) > 1:
twister = AuthServiceProxy(serverUrl) twister = AuthServiceProxy(serverUrl)
class MyDb:
lastBlockHash = 0
try: try:
db = cPickle.load(open(dbFileName)) db = cPickle.load(open(dbFileName))
nextHash = db.lastBlockHash nextHash = db.lastBlockHash
dataLock = db.dataLock
except: except:
db = MyDb() db = MyDb()
nextHash = twister.getblockhash(0) 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) blocksInStep = blocksInStep - 1
db.lastBlockHash = block["hash"] if blocksInStep < 0:
db.dataLock = False
break
blocksInStep = blocksInStep - 1 print "read block", str(block["height"])# + "\r",
if blocksInStep < 0:
break
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"]: cPickle.dump(db, open(dbFileName, "w"))
print "follow", u
twister.follow(nodeUserName, [u])
if block.has_key("nextblockhash"):
nextHash = block["nextblockhash"]
else:
break
cPickle.dump(db,open(dbFileName, "w")) print "task completed."
print "task completed." else:
print "operation locked by the running process."

Loading…
Cancel
Save