aboutsummaryrefslogtreecommitdiff
path: root/mensa/logic.py
diff options
context:
space:
mode:
Diffstat (limited to 'mensa/logic.py')
-rwxr-xr-xmensa/logic.py68
1 files changed, 52 insertions, 16 deletions
diff --git a/mensa/logic.py b/mensa/logic.py
index f14b89c..75b2465 100755
--- a/mensa/logic.py
+++ b/mensa/logic.py
@@ -3,7 +3,13 @@
from mensa import base
import urllib.error
import os
+import sys
from yapsy.PluginManager import PluginManager
+try:
+ import multiprocessing
+ parallel=True
+except:
+ parallel = False
def init_foodsources():
backends = PluginManager()
@@ -22,25 +28,55 @@ def init_renderers():
pluginInfo.plugin_object.register_renderer()
-def get_food(restlist=False, options={}) :
+def get_food(restlist=False, **options) :
foodl = []
- for k,i in base.foodsources.items() :
- if restlist and not i.name in restlist :
- continue
- try :
- food = i.get_food(ignore_nudelauswahl=True)
- foodl.append((i, food))
- # print("*"*20+i.human_name+"*"*20+"\n"+base.formt(food))
- except base.NoMenuError:
- print(i.human_name + ": No menu found. This could be due to a holiday or due to an error in the script.")
- except urllib.error.HTTPError as e :
- print(i.human_name + ": Fetching menu failed: %s" % str(e))
- except :
- print(i.human_name + ": Unknown error")
+ if parallel :
+ r = []
+ for k,i in base.foodsources.items() :
+ if restlist and not i.name in restlist :
+ continue
+ r.append(i)
+
+ foodl = get_food_parallel(r, ignore_nudelauswahl=True)
+ else :
+ for k,i in base.foodsources.items() :
+ if restlist and not i.name in restlist :
+ continue
+ try :
+ food = i.get_food(ignore_nudelauswahl=True)
+ foodl.append((i, food))
+ # print("*"*20+i.human_name+"*"*20+"\n"+base.formt(food))
+ except base.NoMenuError:
+ sys.stderr.write(i.human_name + ": No menu found. This could be due to a holiday or due to an error in the script.\n")
+ except urllib.error.HTTPError as e :
+ sys.stderr.write(i.human_name + ": Fetching menu failed: %s\n" % str(e))
+ except :
+ sys.stderr.write(i.human_name + ": Unknown error\n")
return foodl
-def render(to_render, rendlist=False, options={}) :
+# format:
+# if __name__ == "__main__":
+# pool = multiprocessing.Pool(4)
+# k = pool.map(pr_f, list(mensenliste.items()))
+# k.sort(key=lambda x: x[0])
+# for i in k :
+# print(i[1])
+def get_food_parallel_helper(i) :
+ try :
+ return (i[0],i[0].get_food(**i[1]))
+ except:
+ sys.stderr.write(i[0].human_name + ": No menu found. This could be due to a holiday or due to an error in the script.\n")
+ return (i[0], [])
+
+def get_food_parallel(rl, **options) :
+ pool = multiprocessing.Pool(60)
+ k = [ (i, options) for i in rl ]
+ l = list(pool.map(get_food_parallel_helper, k))
+ l.sort(key=lambda x: x[0].human_name)
+ return l
+
+def render(to_render, rendlist=False, **options) :
for k,i in base.renderers.items() :
if rendlist and not i.name in rendlist :
continue
- i.render(to_render)
+ i.render(to_render, **options)