我正在编写一个数据挖掘程序,它批量插入用户数据.
当前的SQL只是一个普通的批量插入:
insert into USERS(
id, username, profile_picture)
select unnest(array['12345']),
unnest(array['Peter']),
unnest(array['someURL']),
on conflict (id) do nothing;
Run Code Online (Sandbox Code Playgroud)
如果发生冲突,我该如何更新?我试过了:
...
unnest(array['Peter']) as a,
unnest(array['someURL']) as b,
on conflict (id) do
update set
username = a,
profile_picture = b;
Run Code Online (Sandbox Code Playgroud)
但它会引发There is a column named "a" in table "*SELECT*", but it cannot be referenced from this part of the query.错误.
编辑:
表USERS非常简单:
create table USERS (
id text not null primary key,
username text,
profile_picture text
);
Run Code Online (Sandbox Code Playgroud) 我有一个上传者,当他们超过配额时拒绝用户上传.响应是JSON,如下所示:
{msg: "Upload limit reached", status: "error", code: "403"}
Dropzone JS选项如下:
Dropzone.options.uploadDropzone = {
paramName: "file1",
maxFilesize: 200,
maxThumbnailFilesize: 10,
success: function(file, response){
????
}
};
Run Code Online (Sandbox Code Playgroud)
我应该如何处理响应,success以便在上传器中向我的用户显示错误?
我将构建一个旨在"查看"很多的页面,但更少的用户将"写入"数据库.例如,100个用户中只有1个可以在我的网站上发布他的新闻,其余的只会阅读新闻.
在上述情况下,当他们访问我的主页时,将执行100个相同的查询,而实际的数据库更改很少.实际上,其中99个查询是浪费计算机能力.是否有任何方法可以缓存第一个查询的结果,当它们在短时间内检测到相同的查询时,是否可以提供缓存的结果?
我使用MongoDB和Tornado.但是,有些帖子说MongoDB没有进行缓存.
使用像Nginx这样的静态缓存HTML不是首选,因为我想每次都通过Tornado呈现个性化页面.
要将值存储在DOM元素中,我们可以通过data属性来完成
$("#abc").data("item", 1),检索做 $("#abc").data("item")
但今天我了解到我们也可以这样做:
$("#abc")[0].item = 1,回头做 $("#abc)[0].item
它们之间有什么区别?哪一个更好?哪一个获得更广泛的兼容性?
请求处理程序如下:
class TestHandler(tornado.web.RequestHandler): # localhost:8888/test
@tornado.web.asynchronous
def get(self):
t = threading.Thread(target = self.newThread)
t.start()
def newThread(self):
print "new thread called, sleeping"
time.sleep(10)
self.write("Awake after 10 seconds!")
self.finish()
class IndexHandler(tornado.web.RequestHandler): # localhost:8888/
def get(self):
self.write("It is not blocked!")
self.finish()
Run Code Online (Sandbox Code Playgroud)
当我获取时localhost:8888/test,页面加载10秒并显示Awake after 10 seconds; 在加载时,如果我localhost:8888/index在新的浏览器选项卡中打开,则不会立即阻止和加载新的索引页面.这些符合我的期望.
但是,在/test加载时,如果我/test在新的浏览器选项卡中打开另一个,则会被阻止.第二个/test仅在第一个完成后开始处理.
我在这里犯了什么错误?
为什么try ... catch不能用于下面的示例代码?
const http2 = require("http2")
const url = require("url")
function sendRequest() {
return new Promise((resolve, reject) => {
var r = http2.request({
"host": "www.google.com",
"method": "GET",
"path": "/"
}, (resp) => {
var data = []
resp.on("data", (chunk) => {
throw ("Error")
})
resp.on("end", () => {
console.log("ended")
resolve("finished")
})
resp.on("error", (e) => {
console.log("error")
reject(e)
})
})
r.end()
})
}
async function wrapper(){
try {
console.log("Sending request")
await sendRequest()
console.log("Finished sending Request")
}catch(e){
console.log("error!") // Supposed to …Run Code Online (Sandbox Code Playgroud) 我知道这synchronize(LOCK)是不公平的,这意味着不能保证等待时间最长的线程会赢得锁。然而,在我下面的小实验中,似乎锁是由最短的等待线程获取的......
public class Demo {
public static final Object LOCK = new Object();
public void unfairDemo(){
// Occupy the lock for 2 sec
new Thread(() -> {
synchronized (LOCK) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
// Spawn 10 new threads, each with 100ms interval, to see which can win the lock
// If lock is fair then it should print the i in asc order
for (var i = 0; i …Run Code Online (Sandbox Code Playgroud) 我有:
def readDb():
# Fetch a lot of data from db, spends a lot time
...
return aList
def calculation():
x = readdb()
# Process x
...
return y
Run Code Online (Sandbox Code Playgroud)
在python解释器中,
每次运行calculation()它都需要花费大量时间来重新读取数据库,这是不必要的.
如何存储结果readdb()以避免此缩减过程?
编辑:
我在这里发现了一个类似的问题,但我不太清楚答案
保存函数重新使用而不重新执行
如果我创建以下复合索引:
{'username': 1, 'uid': 1}
可以利用该索引中使用,如果我只跟搜索uid一样
db.users.find({'uid': '12345'})?
因为生成两个单独的单个索引似乎会占用更多内存。
我正在尝试将我的照片与参数中发送的标签(逗号分隔的字符串)一起上传tagname.
sending传递给Dropzone.JS 的选项允许我在发送请求之前获取XHR对象.
Dropzone.options.uploadDropzone({
// ...
sending: function(file, xhr, formData){
// but how to add my tags string to the params?
// any methods like setting the header:
// xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest")?
}
})
Run Code Online (Sandbox Code Playgroud) javascript ×4
dropzone.js ×2
mongodb ×2
python ×2
tornado ×2
asynchronous ×1
caching ×1
html ×1
indexing ×1
java ×1
jquery ×1
jvm ×1
locking ×1
node.js ×1
postgresql ×1
python-2.7 ×1
sql ×1
upsert ×1