@numba.njit
def med(data):
res = np.full_like(data, np.nan)
state = init(data[:windowsize])
res[halfsize] = func(*state)
for i in range(windowsize, data.size):
xin,xout = data[i], data[i - windowsize]
state = update(*state, xin, xout)
res[i-halfsize] = func(*state)
return res
这是我的上传图片的完整功能。我唯一的问题是该函数内部具有回调函数。
import pandas
data=pandas.DataFrame(np.random.rand(10**5))
%time res1=data[0].rolling(window = windowsize, center = True).apply(funcX, raw = True)
Wall time: 10.8 s
res2=med(data[0].values)
np.allclose((res1-res2)[halfsize:-halfsize],0)
Out[112]: True
%timeit res2=med(data[0].values)
40.4 ms ± 462 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
仅此部分可以获取异步等待功能?
答案 0 :(得分:2)
如果有人在使用 await/async 而不是回调时需要同时观察上传进度,则此代码有效:
try {
await saveToFirebase();
alert("Happy days!");
} catch (error) {
alert("Sad days!");
}
async function saveToFirebase() {
// Create promise.
let promise = $.Deferred();
// Set storage reference.
let storageRef = firebase.storage().ref();
// Se file path.
let filepath = "your-path.txt";
// Set content type.
let metadata = {
contentType: "text/plain",
cacheControl: "no-cache, max-age=0"
};
// Start upload.
let uploadTask = storageRef.child(filepath).putString(data, "raw", metadata);
// Monitor state of upload operation.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, function(snapshot) {
// Get task progress, including number of bytes uploaded and total number of bytes to be uploaded.
let progress = (snapshot.bytesTransferred / snapshot.totalBytes);
// Display progress to user.
// Error during upload?
}, function(error) {
promise.reject(error);
// Nope, upload succeeded.
}, function() {
promise.resolve();
});
// Return promise.
return promise;
}
答案 1 :(得分:1)
您可以很好地在then()
上致电UploadTask
,如下所述:https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask#then
此对象的行为类似于Promise,并通过其快照进行解析 上传完成后保存数据。
因此您可以执行以下操作:
async UPLOAD_IMAGES() {
try {
let self = this;
const storageRef = firebase.storage().ref();
const files = document.getElementById("photoupload").files;
const file = files[0];
// Create the file metadata
const metadata = {
contentType: "image/jpeg"
};
const fileRef = storageRef
.child(`${this.name}/` + file.name);
const uploadTaskSnapshot = await fileRef.put(file, metadata);
const downloadURL = await uploadTaskSnapshot.ref.getDownloadURL();
self = downloadURL;
} catch (error) {
console.log("ERR ===", error);
alert("Image uploading failed!");
}
}