소스 검색

Nawak: update to newest version with threads

Erwan Ameil 11 년 전
부모
커밋
94a6fdadac
4개의 변경된 파일36개의 추가작업 그리고 23개의 파일을 삭제
  1. 1 1
      nawak/fortunes_tmpl.nim
  2. 15 0
      nawak/model.nim
  3. 17 18
      nawak/nawak_app.nim
  4. 3 4
      nawak/setup.py

+ 1 - 1
nawak/fortunes_tmpl.nim

@@ -1,6 +1,6 @@
 #! stdtmpl | standard
 #import lib/escape
-#from nawak_app import TFortune
+#import model
 #proc fortunes_tmpl*(fortunes: openArray[TFortune]): string =
 #  result = ""
 <!DOCTYPE html>

+ 15 - 0
nawak/model.nim

@@ -0,0 +1,15 @@
+
+type THello* = tuple[message: string]
+type TWorld* = tuple[id: int, randomNumber: int]
+type TFortune* = tuple[id: int, message: string]
+
+
+import strutils
+import lib/db_postgres_redone
+
+proc unrowTWorld*(x: TRow): TWorld =
+    result.id = parseInt(x[0])
+    result.randomNumber = parseInt(x[1])
+
+proc unrowTFortune*(x: TRow): TFortune =
+    return (x[0].parseInt, x[1])

+ 17 - 18
nawak/nawak_app.nim

@@ -1,5 +1,6 @@
 import strtabs, strutils, math, algorithm
 import nawak_mongrel, jdump
+import model, fortunes_tmpl
 
 # The following import belongs to the stdlib, but has been updated to support
 # queries with parameters (that are safer to counter SQL injections) and
@@ -7,34 +8,28 @@ import nawak_mongrel, jdump
 # It will be merged eventually. For now, I included it in the repository.
 import lib/db_postgres_redone
 
-type THello = tuple[message: string]
-type TWorld = tuple[id: int, randomNumber: int]
-type TFortune* = tuple[id: int, message: string]
 
-proc unrowTWorld(x: TRow): TWorld =
-    result.id = parseInt(x[0])
-    result.randomNumber = parseInt(x[1])
-proc unrowTFortune(x: TRow): TFortune =
-    return (x[0].parseInt, x[1])
-
-import fortunes_tmpl  # Needs TFortune to be defined first
-
-var db = open("", "benchmarkdbuser", "benchmarkdbpass", "host=localhost port=5432 dbname=hello_world")
+var db {.threadvar.}: TDbConn
+var qworld_prepared {.threadvar.}: TPreparedId
+var qfortunes_prepared {.threadvar.}: TPreparedId
+var qupdates_prepared {.threadvar.}: TPreparedId
 
 const qworld = "SELECT id, randomNumber FROM World WHERE id = $1"
 const qfortunes = "SELECT id, message FROM Fortune"
 const qupdates = "UPDATE World SET randomNumber = $1 WHERE id = $2"
 
-# prepare queries
-let qworld_prepared = db.prepare("world", qworld, 1)
-let qfortunes_prepared = db.prepare("fortunes", qfortunes, 0)
-let qupdates_prepared = db.prepare("updates", qupdates, 2)
+proc init() =
+    db = open("", "benchmarkdbuser", "benchmarkdbpass", "host=localhost port=5432 dbname=hello_world")
+    # prepare queries
+    qworld_prepared = db.prepare("world", qworld, 1)
+    qfortunes_prepared = db.prepare("fortunes", qfortunes, 0)
+    qupdates_prepared = db.prepare("updates", qupdates, 2)
 
 
 get "/json":
     var j: THello
     j.message = "Hello, World!"
-    # jdump serialize any tuple as json
+    # jdump serialize the tuples of the model as json
     return response(jdump(j), "application/json")
 
 get "/plaintext":
@@ -99,5 +94,9 @@ get "/updates":
 
     return response(jdump(world), "application/json")
 
+custom_page 404:
+    # customize the content of the 404 page
+    return response(404, """Nah, I've got nothing.<br>
+                            Here's a <b>404 Page Not Found</b> error for you.""")
 
-run()
+run(init=init, nb_threads=256)

+ 3 - 4
nawak/setup.py

@@ -9,16 +9,15 @@ home = expanduser("~")
 def start(args, logfile, errfile):
   # compile the app
   setup_util.replace_text("nawak/nawak_app.nim", "host=.* port=5432", "host=" + args.database_host + " port=5432")
-  subprocess.check_call("nimrod c -d:release --path:../installs/nawak/nawak nawak_app.nim",
+  subprocess.check_call("nimrod c --threads:on -d:release --path:../installs/nawak/nawak nawak_app.nim",
                         shell=True, cwd="nawak", stderr=errfile, stdout=logfile)
   # launch mongrel2
   subprocess.check_call("mkdir -p run logs tmp", shell=True, cwd="nawak/conf", stderr=errfile, stdout=logfile)
   subprocess.check_call("sudo m2sh load -config mongrel2.conf", shell=True, cwd="nawak/conf", stderr=errfile, stdout=logfile)
   subprocess.check_call("sudo m2sh start -name test", shell=True, cwd="nawak/conf", stderr=errfile, stdout=logfile)
   
-  for i in range(1, 43):
-    # launch workers
-    subprocess.Popen("./nawak_app " + str(i), shell=True, cwd="nawak", stderr=errfile, stdout=logfile)
+  # launch workers
+  subprocess.Popen("./nawak_app", shell=True, cwd="nawak", stderr=errfile, stdout=logfile)
   return 0
 
 def stop(logfile, errfile):