dom96 / jester

A sinatra-like web framework for Nim.
MIT License
1.57k stars 120 forks source link

why the browser window to send the request is the execution order, rather than asynchronous execution. #52

Closed lihf8515 closed 8 years ago

lihf8515 commented 8 years ago

My code is as follows, open multiple browser windows, access program written by Jester the same address, submits the query to it, code to query the database statements, depending on the query condition, query time, ranging from the length of, why the browser window to send the request is the execution order, rather than asynchronous execution.

import db_odbc
import jester, asyncdispatch, htmlgen, asyncnet

settings:
  port = Port(5000)
  appName = "/book"
  bindAddr = "127.0.0.1"

routes:
  get "/":
    resp "Forbidden access!"

  post "/":
    resp "Forbidden access!"

  post "/cx":
    var mysql="select goods.goodsid,goods.categoryid,goods.barcode,goods.goodslname,goods.adviceprice,"
    mysql.add "goods.cbsj,goods.bb,shopstock.qty from goods left join shopstock on goods.goodsid=shopstock.goodsid where 1=1"
    var isbn,sm,cbs:string
    isbn=""
    sm=""
    cbs=""
    isbn=request.formData.getOrDefault("t_isbn").body
    sm=request.formData.getOrDefault("t_sm").body
    cbs=request.formData.getOrDefault("t_cbs").body
    if isbn=="" and sm=="" and cbs=="":
      resp "nokey","text/html;charset=gb2312"
    else:
      if isbn!="":
        mysql.add " and goods.barcode='" & isbn & "'"
      if sm!="":
        mysql.add " and goods.goodslname like '%" & sm & "%'"
      if cbs!="":
        mysql.add " and goods.bb like '%" & cbs & "%'"

      var theDb = open("dbpos", "dbcxj", "dbcxj", "dbpos")
      var myrows :seq[Row] = @[]
      myrows=theDb.getAllRows(sql(mysql))
      theDb.close()

      resp $myrows,"text/html;charset=gb2312"

runForever()
dom96 commented 8 years ago

Sorry, I'm not sure what you mean. Keep in mind that db_odbc is not asynchronous.

lihf8515 commented 8 years ago

That I in "jester" how to deal with the database query and return value?

lihf8515 commented 8 years ago

You mean "post" a block of statements if there is a time-consuming operation. The it has been wait until the operation is complete, continue to respond to requests from other clients?? if so, then how can it is asynchronous. Seems to be synchronized.

dom96 commented 8 years ago

I'm sorry, I'm still not sure what you mean. Not all modules support asynchronous operation. The db_odbc does not support asynchronous operation. You would need to write one which does. Jester supports asynchronous operation.

lihf8515 commented 8 years ago

Perhaps I have a problem with asynchronous understanding, can you give me a test code to see it, I want to deepen the understanding of asynchronous. Especially for 'async/await' does not understand, thank you!