首先让我展示一下有效的方法.如果我使用flock与文件路径,它的工作原理.
1号航站楼:
[root@centos ~]# flock -x -n /tmp/foo.txt -c "sleep 100"
Run Code Online (Sandbox Code Playgroud)
2号航站楼:
[root@centos ~]# flock -x -n /tmp/foo.txt -c "sleep 100"
[root@centos ~]# echo $?
1
Run Code Online (Sandbox Code Playgroud)
上面的输出显示我首先获得了第一个终端中/tmp/foo.txt的独占锁.然后在第二个终端中,当我尝试获取同一文件的锁时,它失败了.
现在让我知道什么是行不通的.如果我使用文件描述符flock,它不起作用.
1号航站楼:
[root@centos ~]# { flock -x -n 100; sleep 100; } 100> /tmp/foo.txt
Run Code Online (Sandbox Code Playgroud)
2号航站楼:
[root@centos ~]# { flock -x -n 100; sleep 100; } 100> /tmp/foo.txt
Run Code Online (Sandbox Code Playgroud)
以上输出显示我首先尝试/tmp/foo.txt在第一个终端获取锁定.然后在第二个终端中,当我尝试获取同一文件的锁时,它成功.我预计它会像上一个例子中那样失败.为什么会成功?
这是我写的示例代码.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
int main()
{
int fd;
long pagesize;
char *data;
if ((fd = open("foo.txt", O_RDONLY)) == -1) {
perror("open");
return 1;
}
pagesize = sysconf(_SC_PAGESIZE);
printf("pagesize: %ld\n", pagesize);
data = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd, 0);
printf("data: %p\n", data);
if (data == (void *) -1) {
perror("mmap");
return 1;
}
printf("%d\n", data[0]);
printf("%d\n", data[1]);
printf("%d\n", data[2]);
printf("%d\n", data[4096]);
printf("%d\n", data[4097]);
printf("%d\n", data[4098]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我向该程序提供零字节foo.txt,它将以SIGBUS终止.
$ > foo.txt && …Run Code Online (Sandbox Code Playgroud) 看一下这个 shell 会话,我在其中用 Go 构建了一个简单的 hello world 程序。
$ cd ~/lab/hello/
$ ls
hello.go
$ cat hello.go
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
$ go build
$ ./hello
hello, world
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.7 (jessie)
Release: 8.7
Codename: jessie
Run Code Online (Sandbox Code Playgroud)
这是我不明白的地方。https://golang.org/doc/install#testing上的教程说我应该将hello.go文件放在 …
在 macOS High Sierra 10.13.6 上,我vim在终端中启动并在 Vim 中输入以下命令。
:e foo.txt
:e bar.txt
Run Code Online (Sandbox Code Playgroud)
现在,如果我按Ctrl+ 6,它不会返回到备用文件foo.txt。
Ctrl+ Shift+6工作正常并且确实返回到备用文件foo.txt。
Ctrl我使用过的所有其他 Vim、MacVim 或 GVim 都会在按+时返回到备用文件6。为什么它不能与vimmacOS 一起使用?
这是我的 vim 版本详细信息:
$ which vim
/usr/bin/vim
$ vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Nov 29 2017 18:37:46)
Included patches: 1-503, 505-680, 682-1283
Compiled by root@apple.com
Normal version without GUI. Features included (+) or not …Run Code Online (Sandbox Code Playgroud) HTML代码:
<div id="container">
<div id="first">
foo bar baz foo bar baz
foo bar baz foo bar baz
</div>
<div id="second">
Foo
</div>
<div id="third">
foo bar baz foo bar baz
foo bar baz foo bar baz
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS代码:
body {
text-align: center;
}
#container {
display: inline-block;
background: lightcyan;
}
#first {
display: inline-block;
background: lightgreen;
}
#second {
background: orange;
}
#third {
width: 30%;
display: inline-block;
background: lightblue;
}
Run Code Online (Sandbox Code Playgroud)
我试图确保整个div#容器收缩包装div#first.上面的代码按预期工作.看这个演示:http://jsfiddle.net/HhrE6/
但是,当我在div#third中添加更多文本时,收缩包装会中断.查看破解的演示:http://jsfiddle.net/n4Kn2/ …
这是我的代码似乎表明答案是肯定的 - http://jsfiddle.net/4nKqu/
var Foo = function() {
'use strict'
return {
foo: function() {
a = 10
alert('a = ' + a)
}
}
}()
try {
Foo.foo()
} catch (e) {
alert(e)
}
Run Code Online (Sandbox Code Playgroud)
您能否引用标准中的陈述,阐明该陈述'use strict'是否自动应用于我们应用的函数中定义的所有闭包和函数'use strict'?
请参阅以下程序.
#include <stdio.h>
void f(int a);
int main()
{
f(10);
return 0;
}
void f(const int a)
{
/* a = 20; */ /* Want to avoid accidental modification of a. */
printf("%d\n", a);
}
Run Code Online (Sandbox Code Playgroud)
在该程序中,函数的声明f()与定义不完全匹配.声明具有int a参数,但定义具有const int a参数.
以下是关于此计划的问题.
f()不必知道实际参数a是否被视为常量f().这一点细节是私密的f().这不是调用代码的关注点.就调用者而言,在任何一种情况下,int a在调用时它都可以改变f().我对吗?const int或者将const char *const它们在标题中声明为just int或const char *?如果没有,你推荐这种方式吗?为了保持第二个问题的目标,请列出这样做的利弊.在下面的代码中,我没有使变量quit具有volatile sig_atomic_t.我把它留作平原int.
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#define UNUSED(x) (void) (x)
int quit;
void sigusr1_handler(int sig)
{
UNUSED(sig);
write(1, "handler\n", 8);
quit = 1;
}
int main()
{
struct sigaction sa;
sa.sa_handler = sigusr1_handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGUSR1, &sa, NULL) == -1) {
perror("sigaction");
return 1;
}
quit = 0;
while (!quit) {
printf("Working ...\n");
sleep(1);
}
printf("Exiting ...\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于未将quit变量指定为volatile,我期望编译器的优化器会优化while代码中的-loop: …
注意:这个问题不是其他现有问题的重复,因为这个问题不使用jsdom.env()旧版本的 JSDOM 使用的函数调用。
文件bar.js:
console.log('bar says: hello')
Run Code Online (Sandbox Code Playgroud)
文件foo.js:
var jsdom = require('jsdom')
var html = '<!DOCTYPE html><head><script src="bar.js"></script></head><body><div>Foo</div></body>'
var window = new jsdom.JSDOM(html).window
window.onload = function () {
console.log('window loaded')
}
Run Code Online (Sandbox Code Playgroud)
当我运行时foo.js,我得到这个输出。
$ node foo.js
window loaded
Run Code Online (Sandbox Code Playgroud)
为什么bar says: hello输出没有来?好像bar.js没有加载。如何jsdom在script标签中加载文件?
[编辑/解决方案]:在遵循 Quentin 的答案中的建议后问题已解决。此代码有效:
var jsdom = require('jsdom')
var html = '<!DOCTYPE html><head><script src="bar.js"></script></head><body><div>Foo</div></body>'
var window = new jsdom.JSDOM(html, { runScripts: "dangerously", resources: …Run Code Online (Sandbox Code Playgroud) 的文档concurrent.futures.ThreadPoolExecutor说:
改变在3.5版本中:如果max_workers是
None或者没有给出,将默认为机器上的处理器,乘以数量5,假设的ThreadPoolExecutor通常用于重叠I / O,而不是CPU的工作,工人的数量应该更高比ProcessPoolExecutor的工人数量。
我想了解为什么默认max_workers值取决于 CPU 的数量。不管我有多少 CPU,在任何时间点都只能运行一个 Python 线程。
让我们假设每个线程都是 I/O 密集型的,它只有 10% 的时间在 CPU 上,90% 的时间在等待 I/O。然后让我们假设我们有 2 个 CPU。我们只能运行 10 个线程来使用 100% 的 CPU。我们不能再使用 CPU,因为在任何时间点都只有一个线程在运行。即使有 4 个 CPU,也是如此。
那么为什么默认是max_workers根据 CPU 数量来决定的呢?
c ×3
javascript ×2
arguments ×1
concurrency ×1
const ×1
cpu ×1
css ×1
ecmascript-5 ×1
gil ×1
go ×1
html ×1
jsdom ×1
linux ×1
locking ×1
macos ×1
mmap ×1
node.js ×1
performance ×1
python ×1
shell ×1
sig-atomic-t ×1
sigbus ×1
signals ×1
strict ×1
terminal ×1
use-strict ×1
vim ×1
volatile ×1