summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2010-05-19 14:33:59 -0400
committerGuillaume Seguin <guillaume@segu.in>2010-05-19 14:33:59 -0400
commit3b530af71f5db0e07241ba2966880b9cb64f50c1 (patch)
tree270395eb0e7699866ca8c4d34a1e64cadb386aa9
parent0feff42c4d4c7f85d42f896567dca1773e3b3fab (diff)
downloadsqwareapi-3b530af71f5db0e07241ba2966880b9cb64f50c1.tar.gz
sqwareapi-3b530af71f5db0e07241ba2966880b9cb64f50c1.tar.bz2
Add multithreaded status dumper
-rw-r--r--multistatus.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/multistatus.py b/multistatus.py
new file mode 100644
index 0000000..89506e4
--- /dev/null
+++ b/multistatus.py
@@ -0,0 +1,72 @@
+import sqwarea
+import sys
+import cPickle as pickle
+import gzip
+
+from multiprocessing import Pool
+PROCESS_COUNT = 15
+
+openids = sys.argv[1:]
+if not openids:
+ import myids
+ openids = [login
+ for login in myids.logins
+ if "http" in login]
+ openids += [myids.basepath % login
+ for login in myids.logins
+ if "http" not in login]
+
+def get_status_wrapper (openid):
+ try:
+ return get_status (openid)
+ except KeyboardInterrupt:
+ processing_pool.terminate ()
+ raise KeyboardInterrupt
+
+def get_status (openid):
+ print "Fetching %s status" % openid
+ api = sqwarea.API (openid)
+ ongoingAttacks, incomingAttacks = None, None
+ if api.ongoingAttacks:
+ ongoingAttacks = api.ongoingAttacks.values ()
+ if api.incomingAttacks:
+ incomingAttacks = api.incomingAttacks.values ()
+ return openid, api.king, api.academyTroops, \
+ ongoingAttacks, incomingAttacks
+
+if __name__ == '__main__':
+ processing_pool = Pool (PROCESS_COUNT)
+
+ incoming = {}
+ ongoing = {}
+ kings = {}
+
+ print "Fetching statuses"
+ try:
+ statuses = processing_pool.map (get_status_wrapper, openids)
+ except KeyboardInterrupt:
+ processing_pool.terminate ()
+ raise
+
+ print "Processing statuses"
+ for status in statuses:
+ openid, king, troops, ongoingAttacks, incomingAttacks = status
+ kings[king.pos.p] = (openid, king.id, troops)
+ if ongoingAttacks:
+ for attack in ongoingAttacks:
+ if attack.dest.p not in ongoing:
+ ongoing[attack.dest.p] = []
+ ongoing[attack.dest.p].append ((openid, attack))
+ if incomingAttacks:
+ for attack in incomingAttacks:
+ if attack.dest.p not in incoming:
+ incoming[attack.dest.p] = []
+ incoming[attack.dest.p].append ((openid, attack))
+
+ print "Dumping statuses"
+ out_filename = "status.dump"
+ f = gzip.open (out_filename, "wb")
+ pickle.dump (incoming, f, -1)
+ pickle.dump (ongoing, f, -1)
+ pickle.dump (kings, f, -1)
+ f.close ()