简体   繁体   中英

Node.js express, mysql and non-blocking behaviour

I have mysql database with some content in it. Once a minute, i request from some api a new content. After requesting i add that new content (which is take about 50 sql queries) to my database. The problem is that i can't request content from database, before this 50 sql queries finish adding. I mean web browser doesn't answer. Is there any solution to do this 50 sql queries go asynchronously and no block the browser.

Check out the Mysql2 node package for node if you haven't already. It support creating pools of connections, meaning that you can do multiple calls to your database simultaneously, which I think is what you are looking for. It also have wrappers for working with promises within your code to support async operations.

To be able to make async simultaneous calls you need both of the things above:

  1. Having a "pool" of connections from your client to the DB - think of it like spiderwebs that transfer information. If you only have one, it will be busy when you are doing a call, if you have multiple you can transfer information simultaneously.

  2. Your code must be adapted for async operations, meaning that if one line take extra time, it will jump to the next one and start with that one. If you are going to work with promises I recommend you looking into the Promise.all() method which let you perform simultaneous operations related to promises.

https://www.npmjs.com/package/mysql2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM