我正在启动一个协程,我希望它在我恢复执行主线程之前完成。
我的代码简化如下:
fun hello() {
for (i in 0..100) {
println("hello")
}
}
fun main(args: Array<String>) {
val job = GlobalScope.launch { hello() } //launch parallel
GlobalScope.launch { job.join() } //try to wait for job to finish
print("done")
}
Run Code Online (Sandbox Code Playgroud)
问题是,因为job.join()需要在一个协程内,执行的主线被推迟到“完成”,所以输出看起来像这样:
donehello
hello
hello
hello
Run Code Online (Sandbox Code Playgroud)
我想等待工作完成,就像sync.WaitGroup在 Go 中使用一样。所以我的输出肯定是这样的:
hello
hello
hello
hello
...
done
Run Code Online (Sandbox Code Playgroud)
我该如何实现?
我有一个采用 csv 文件的端点。
现在,我想编写一个测试,使用此文件发出发布请求。
我正在尝试动态生成此 csv 文件(而不是手动创建和存储它)
我试过这个:
def csv_fixture(rows, type):
headers = None
if type == "merchant_upload":
headers = MerchantCSV.ordered_columns()
elif type == "invoice_upload":
headers = InvoiceCSV.ordered_columns()
assert headers is not None
rows = [headers] + rows
with open("file.csv", "w+") as f:
writer = csv.writer(f)
writer.writerows(rows)
yield f
my_file = csv_fixture(merchants, type="merchant_upload")
request = rf.post("/invoice_admin/upload_organisations/",
{"onboarding_file": my_file})
Run Code Online (Sandbox Code Playgroud)
我的端点做了这样的事情:
if filename not in request.FILES:
raise Exception("Upload Failed: No file submitted.")
file = TextIOWrapper(
request.FILES[filename].file, encoding=request.encoding)
headers = peek_first_row(file)
missing = …Run Code Online (Sandbox Code Playgroud) 我在 ubuntu 18.04 上,我想升级我的 intellij 社区版本。
我知道我可以去网站手动下载二进制文件,但我有 githount 帐户同步到我的 2018 首选项和设置。
因此,如果我可以在不实际删除 2018 版本的情况下进行升级,那就更好了。
有什么办法可以做到这一点吗?(我还没有通过 snap 安装 intellij——这样sudo snap refresh intellij-idea-community做没有用)
在 python 中,如果我这样做:
a = []
b = a
a.append(1)
b[0] == 1
Run Code Online (Sandbox Code Playgroud)
这很好用,因为 a 和 b 都指向堆上的底层对象。
什么是等效的 C++ 代码?
当我 ^R在终端上进行反向搜索时,我得到以下信息:
(^R) was pressed. Waiting for second key of chord...
我该如何解决?我在 OS X 上。
我正在使用 clion 并且正在运行一些具有 UB 的代码。我的目标是静态捕获它:
#include <iostream>
#include <vector>
int main() {
auto v = std::vector<int>();
v.push_back(20);
auto &first = v[0];
auto vector_ref = &v;
vector_ref->clear();
std::cout << first;
}
Run Code Online (Sandbox Code Playgroud)
这是UB,我正试图抓住它。
我已将以下内容添加到我的 cmake 项目中:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -O1 -fno-omit-frame-pointer -g")
Run Code Online (Sandbox Code Playgroud)
我仍然没有收到警告。
我必须启用什么才能捕获此类 UB 实例?
我不太明白为什么会这样编译:
fn main() {
let mut v = vec![1,2,3];
for i in 1..v.len() {
v[i] = 20;
}
}
Run Code Online (Sandbox Code Playgroud)
...而这不是:
fn main() {
let mut v = vec![1,2,3];
for (i,_) in v.iter().enumerate() {
v[i] = 20;
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
fn main() {
let mut v = vec![1,2,3];
for i in 1..v.len() {
v[i] = 20;
}
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我们都是不可变的借用(一个在调用时len(),另一个在调用时iter())。
因此,我的期望是不应该编译第一个代码段-当存在不可变的借位时,我们在进行赋值时会进行可变的借位。
我有什么误会?
我有一个这样定义的圆:
class Circle {
public:
int x;
int y;
int r;
Circle() : x(0), y(0), r(0) {}
Circle(int x, int y, int r) {
this->x = x;
this->y = y;
this->r = r;
}
double area() {
return PI * pow(r, 2);
}
};
Run Code Online (Sandbox Code Playgroud)
我希望能够根据其中心(x和y)的哈希将其添加到集合中
在 C++ 11 中执行此操作的正确且惯用的方法是什么?
我的问题有两个
(1) 有没有办法让 C++ 帮我哈希它?在 Kotlin 中,有一个数据类的概念,它会自动对类属性进行哈希处理。我正在寻找类似的东西。
(2) 如果没有,我如何自己对其进行哈希处理,以及相关的 - 我需要重载哪些运算符?
我正在尝试解析一个单词,该单词两侧都有空格或标点符号。
我试过这个:
fun main(args: Array<String>) {
val regex = "\bval\b".toRegex();
regex.matches("fn foo() { val x = 2;} x;").also { println(it) }
}
Run Code Online (Sandbox Code Playgroud)
但这打印出来是错误的。我在这里测试了正则表达式https://regex101.com/r/vNBefF/2并且它有效,与输入字符串匹配。
我究竟做错了什么?
is_some()所以我正在查看for的 impl Option,我注意到它match *self {}在幕后使用......所以它在内部移动它。
我的问题是,如果它被移动,我怎样才能做这样的事情?https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f094da12290b77bad526674467e51043
fn main() {
let x = Option::Some(3);
x.is_some();
x.is_some();
}
Run Code Online (Sandbox Code Playgroud)
我的期望是我应该只能调用is_some()一次,而下次调用它时,我应该会收到某种错误,说明它已被移动......但是不,一切都编译得很好。
我有什么误解吗?