我有一个安装的docker镜像grunt
,但是当我尝试运行它时,我收到一个错误:
Error response from daemon: Cannot start container foo_1: \
exec: "grunt serve": executable file not found in $PATH
Run Code Online (Sandbox Code Playgroud)
如果我以交互模式运行bash,grunt
则可用.
我究竟做错了什么?
这是我的Dockerfile:
# https://registry.hub.docker.com/u/dockerfile/nodejs/ (builds on ubuntu:14.04)
FROM dockerfile/nodejs
MAINTAINER My Name, me@email.com
ENV HOME /home/web
WORKDIR /home/web/site
RUN useradd web -d /home/web -s /bin/bash -m
RUN npm install -g grunt-cli
RUN npm install -g bower
RUN chown -R web:web /home/web
USER web
RUN git clone https://github.com/repo/site /home/web/site
RUN npm install
RUN bower install --config.interactive=false --allow-root …
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,第一次调用foo
是不明确的,因此无法编译.
第二个,+
在lambda之前添加,解析为函数指针重载.
#include <functional>
void foo(std::function<void()> f) { f(); }
void foo(void (*f)()) { f(); }
int main ()
{
foo( [](){} ); // ambiguous
foo( +[](){} ); // not ambiguous (calls the function pointer overload)
}
Run Code Online (Sandbox Code Playgroud)
+
这里的符号是什么?
假设我们正在尝试使用tsc进行性能监控,我们希望防止指令重新排序.
这些是我们的选择:
1: rdtscp
是序列化调用.它可以防止对rdtscp的调用进行重新排序.
__asm__ __volatile__("rdtscp; " // serializing read of tsc
"shl $32,%%rdx; " // shift higher 32 bits stored in rdx up
"or %%rdx,%%rax" // and or onto rax
: "=a"(tsc) // output to tsc variable
:
: "%rcx", "%rdx"); // rcx and rdx are clobbered
Run Code Online (Sandbox Code Playgroud)
但是,rdtscp
仅适用于较新的CPU.所以在这种情况下我们必须使用rdtsc
.但是rdtsc
非序列化,因此单独使用它不会阻止CPU重新排序.
所以我们可以使用这两个选项中的任何一个来防止重新排序:
2:这是一个电话cpuid
然后rdtsc
.cpuid
是一个序列化的电话.
volatile int dont_remove __attribute__((unused)); // volatile to stop optimizing
unsigned tmp;
__cpuid(0, tmp, tmp, tmp, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试重新创建Observer模式,我可以将参数完美地转发给观察者的给定成员函数.
如果我尝试传递具有多个覆盖的成员函数的地址,则无法根据参数推断出正确的成员函数.
#include <iostream>
#include <vector>
#include <algorithm>
template<typename Class>
struct observer_list
{
template<typename Ret, typename... Args, typename... UArgs>
void call(Ret (Class::*func)(Args...), UArgs&&... args)
{
for (auto obj : _observers)
{
(obj->*func)(std::forward<UArgs>(args)...);
}
}
std::vector<Class*> _observers;
};
struct foo
{
void func(const std::string& s)
{
std::cout << this << ": " << s << std::endl;
}
void func(const double d)
{
std::cout << this << ": " << d << std::endl;
}
};
int …
Run Code Online (Sandbox Code Playgroud) 我读的越多,我变得越困惑......我会认为找到一个在c ++中实现的正式正确的mpsc队列是微不足道的.
每当我发现另一次刺痛时,进一步的研究似乎表明存在诸如ABA或其他微妙的竞争条件之类的问题.
许多人都在谈论垃圾收集的必要性.这是我想要避免的.
那里有一个公认的正确的开源实现吗?
我有一个函数可以std::string&
就地修改左值引用,返回对输入参数的引用:
std::string& transform(std::string& input)
{
// transform the input string
...
return input;
}
Run Code Online (Sandbox Code Playgroud)
我有一个辅助函数,它允许在右值引用上执行相同的内联转换:
std::string&& transform(std::string&& input)
{
return std::move(transform(input)); // calls the lvalue reference version
}
Run Code Online (Sandbox Code Playgroud)
请注意,它返回一个右值引用.
我已经阅读了关于返回右值引用的SO的几个问题(这里和这里例如),并得出结论这是不好的做法.
根据我的阅读,似乎共识是因为返回值是 rvalues,再加上考虑RVO,只需按值返回就会有效:
std::string transform(std::string&& input)
{
return transform(input); // calls the lvalue reference version
}
Run Code Online (Sandbox Code Playgroud)
但是,我还读到返回函数参数会阻止RVO优化(例如此处和此处)
这使我相信std::string&
从左值参考版本transform(...)
的std::string
返回值到返回值会发生副本.
那是对的吗?
保留我的std::string&& transform(...)
版本会更好吗?
可以使用sched_setaffinity
将线程固定到cpu,从而提高性能(在某些情况下)
从linux手册页:
限制在单个CPU上运行的进程还可以避免在进程停止在一个CPU上执行然后重新开始在另一个CPU上执行时发生的高速缓存失效导致的性能成本
此外,如果我想要更实时的响应,我可以将该线程的调度程序策略更改为SCHED_FIFO
,并将优先级更高为某个高值(最多sched_get_priority_max
),这意味着所讨论的线程应始终抢占任何其他运行的线程在它准备就绪的cpu上.
但是,此时,在实时线程刚刚抢占的cpu上运行的线程可能已经驱逐了大部分实时线程的1级缓存条目.
我的问题如下:
我这里有它使用一个小的测试应用程序isnan
的<math.h>
:
#include <iostream>
#include <math.h>
int main()
{
double d = NAN;
std::cout << isnan(d) << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据3种不同标准构建和运行:
Run Code Online (Sandbox Code Playgroud)$ g++ -std=c++98 main.cpp; ./a.out 1 $ g++ -std=c++11 main.cpp; ./a.out 1 $ g++ -std=c++14 main.cpp; ./a.out 1
现在我们还包括<cmath>
,并测试两者isnan
和std::isnan
:
#include <iostream>
#include <cmath>
#include <math.h>
int main()
{
double d = NAN;
std::cout << std::isnan(d) << '\n';
std::cout << isnan(d) << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
构建并运行:
C++ 98可以工作
Run Code Online (Sandbox Code Playgroud)$ …
我需要从Python运行rsync命令.这是可能的,如果是这样,我该怎么办?
rsync -Ccavz --delete DJStatic username@website
Run Code Online (Sandbox Code Playgroud)
回答评论
我需要用
rsync -Ccavz --delete DJStatic username@website
Run Code Online (Sandbox Code Playgroud) 我有一个代码,我尝试访问资源,但有时它不可用,并导致异常.我尝试使用上下文管理器实现重试引擎,但我无法处理__enter__
上下文表单上下文管理器中调用者引发的异常.
class retry(object):
def __init__(self, retries=0):
self.retries = retries
self.attempts = 0
def __enter__(self):
for _ in range(self.retries):
try:
self.attempts += 1
return self
except Exception as e:
err = e
def __exit__(self, exc_type, exc_val, traceback):
print 'Attempts', self.attempts
Run Code Online (Sandbox Code Playgroud)
这是一些只引发异常的例子(我希望处理的那个)
>>> with retry(retries=3):
... print ok
...
Attempts 1
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'ok' is not defined
>>>
>>> with retry(retries=3):
... open('/file')
...
Attempts 1
Traceback …
Run Code Online (Sandbox Code Playgroud)