C++ 11 std::future
缺乏一种then
将延续附加到未来的方法.
Boost boost::future
提供了这个,并且有一个例子(我无法运行)
我根本无法编译:
#include <iostream>
#include <string>
#include <boost/thread/future.hpp>
boost::future<int> join2(const std::string& realm) {
boost::promise<int> p;
p.set_value(23);
return p.get_future();
}
int main () {
boost::future<int> f = join2("realm1");
// here, I'd like to use f.then(..)
f.wait();
std::cout << f.get() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
编译时
clang++ -o test5.o -c -std=c++11 -stdlib=libc++ \
-I/home/oberstet/boost_1_55_0 test5.cpp
Run Code Online (Sandbox Code Playgroud)
这拯救了
test5.cpp:30:1: error: unknown type name 'future'
future<int> join(const std::string& realm) {
...
Run Code Online (Sandbox Code Playgroud)
我感觉很愚蠢;)发生了什么事?我正在使用clang 3.4和libc …
考虑以下程序(在CPython 3.4.0b1上运行):
import math
import asyncio
from asyncio import coroutine
@coroutine
def fast_sqrt(x):
future = asyncio.Future()
if x >= 0:
future.set_result(math.sqrt(x))
else:
future.set_exception(Exception("negative number"))
return future
def slow_sqrt(x):
yield from asyncio.sleep(1)
future = asyncio.Future()
if x >= 0:
future.set_result(math.sqrt(x))
else:
future.set_exception(Exception("negative number"))
return future
@coroutine
def run_test():
for x in [2, -2]:
for f in [fast_sqrt, slow_sqrt]:
try:
future = yield from f(x)
print("\n{} {}".format(future, type(future)))
res = future.result()
print("{} result: {}".format(f, res))
except Exception as e:
print("{} exception: {}".format(f, …
Run Code Online (Sandbox Code Playgroud) 我asyncio.Protocol.data_received
在新的Python asyncio模块的回调中执行异步操作时遇到问题.
考虑以下服务器:
class MathServer(asyncio.Protocol):
@asyncio.coroutine
def slow_sqrt(self, x):
yield from asyncio.sleep(1)
return math.sqrt(x)
def fast_sqrt(self, x):
return math.sqrt(x)
def connection_made(self, transport):
self.transport = transport
#@asyncio.coroutine
def data_received(self, data):
print('data received: {}'.format(data.decode()))
x = json.loads(data.decode())
#res = self.fast_sqrt(x)
res = yield from self.slow_sqrt(x)
self.transport.write(json.dumps(res).encode('utf8'))
self.transport.close()
Run Code Online (Sandbox Code Playgroud)
与以下客户一起使用:
class MathClient(asyncio.Protocol):
def connection_made(self, transport):
transport.write(json.dumps(2.).encode('utf8'))
def data_received(self, data):
print('data received: {}'.format(data.decode()))
def connection_lost(self, exc):
asyncio.get_event_loop().stop()
Run Code Online (Sandbox Code Playgroud)
随着self.fast_sqrt
被召唤,一切都按预期工作.
有self.slow_sqrt
,它不起作用.
它也不适用self.fast_sqrt
于@asyncio.coroutine
装饰器data_received
.
我觉得我在这里缺少一些基本的东西.
完整的代码在这里: …
ACE(Bespin successsor)具有代码折叠功能.此外,还有一个事件changeFold,可以在代码折叠或展开时触发.
如何从JavaScript触发/设置代码折叠?
即,从N行开始折叠函数的代码.
我喜欢写在FreeBSD 10.1 C程序实现了一个DTrace使用者使用libdtrace
.
我知道我需要从调用开始dtrace_open()
- 例如我找到了这个旧的演示文稿,但我甚至无法启动,因为没有dtrace.h
安装(仅在系统源代码树中).
安装了共享库,例如/usr/sbin/dtrace
,FreeBSD附带的工具可以充当DTrace使用者,并且此工具链接到/lib/libdtrace.so.2
(通过符号链接指向/usr/lib/libdtrace.so
).
任何基本的例子,包括构建指令(FreeBSD 10.1/clang)都会对我有所帮助.
编写自定义使用者的实际目标是创建一个可在Python和PyPy中使用的基于CFFI的包装器.意思是:上面的C程序只是为了入门,学习然后继续.
CFFI是将PyPy与共享库连接的推荐,现代,高性能方式.
CFFI可用于ABI和API级别.后者需要包含头文件,前者需要声明从lib中使用的东西.
改编自Adam的答案,这是一个适用于FreeBSD 10.1的完整示例.
Makefile
:
all:
cc \
-I /usr/src/cddl/compat/opensolaris/include \
-I /usr/src/cddl/contrib/opensolaris/lib/libdtrace/common/ \
-I /usr/src/sys/cddl/compat/opensolaris \
-I /usr/src/sys/cddl/contrib/opensolaris/uts/common/ \
hello_dtrace.c \
-l dtrace -l proc -l ctf -l elf -l z -l rtld_db -l pthread -l util \
-o hello_dtrace
Run Code Online (Sandbox Code Playgroud)
hello_dtrace.c
:
#include <dtrace.h>
#include <signal.h> …
Run Code Online (Sandbox Code Playgroud) 我正在运行SQL Server 2008 Enterprise Edition,并希望监视以下性能指标,即通过动态管理视图(在SQL中):
对于滑动时间窗口,每个数据库文件的平均/最大读/写I/O等待时间(毫秒).
即:每个数据库文件4个数字:avg read wait,max read wait,avg write wait,max write wait.全部以ms为单位,全部用于一些理智(甚至更好的可配置)滑动时间窗口.
我怎样才能做到这一点?
PS:我查看服务器状态的权限,可以读取sys.dm_os_performance_counters
,sys.database_files
,sys.dm_io_virtual_file_stats
等等等等
PS2:至少有一个工具(SQL Server的Quest Spotlight 7)能够以每个数据库文件的毫秒数提供最大I/O等待.所以必须有一些方法..
Linux内核> = 3.9允许通过设置在内核负载平衡的进程之间共享套接字SO_REUSEPORT
:http://lwn.net/Articles/542629/
如何将它用于类型的套接字AF_UNIX
?
看来,它只适用于TCP,而不适用于Unix域套接字.
这是一个Python测试程序:
import os
import socket
if not hasattr(socket, 'SO_REUSEPORT'):
socket.SO_REUSEPORT = 15
if True:
# using TCP sockets
# works. test with: "echo data | nc localhost 8888"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
s.bind(('', 8888))
else:
# using Unix domain sockets
# does NOT work. test with: "echo data | nc -U /tmp/socket1"
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
try:
os.unlink("/tmp/socket1")
except:
pass
s.bind("/tmp/socket1")
s.listen(1) …
Run Code Online (Sandbox Code Playgroud) 我正在遵循github代码中的基本wamp pubsub示例:
此示例从类中发布消息:
class Component(ApplicationSession):
"""
An application component that publishes an event every second.
"""
def __init__(self, realm = "realm1"):
ApplicationSession.__init__(self)
self._realm = realm
def onConnect(self):
self.join(self._realm)
@inlineCallbacks
def onJoin(self, details):
counter = 0
while True:
self.publish('com.myapp.topic1', counter)
counter += 1
yield sleep(1)
Run Code Online (Sandbox Code Playgroud)
我想创建一个引用,以便我可以从代码中的其他地方通过此连接发布消息,即 myobject.myconnection.publish('com.myapp.topic1', 'My message')
从这个类似的问题来看,答案似乎是在连接时,我需要设置类似的东西self.factory.myconnection = self
.我已经尝试了多次这种排列而没有成功.
出厂设置部分如下:
## create a WAMP application session factory
##
from autobahn.twisted.wamp import ApplicationSessionFactory
session_factory = ApplicationSessionFactory()
## .. and set the session class …
Run Code Online (Sandbox Code Playgroud) 我想使用boost::future
continuation和boost::when_all
/ boost::when_any
.
Boost 主干 - 不是1.55 - 包括后者的实现(模仿此处的提议,即将推出的C++ 14/17和Boost 1.56).
这就是我所拥有的(并且它不能编译):
#include <iostream>
#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#define BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
#include <boost/thread/future.hpp>
using namespace boost;
int main() {
future<int> f1 = async([]() { return 1; });
future<int> f2 = async([]() { return 2; });
auto f3 = when_all(f1, f2);
f3.then([](decltype(f3)) {
std::cout << "done" << std::endl;
});
f3.get();
}
Run Code Online (Sandbox Code Playgroud)
Clang 3.4拯救了这个 - 这是一个摘录:
/usr/include/c++/v1/memory:1685:31: error: call to deleted constructor of …
Run Code Online (Sandbox Code Playgroud) python ×4
boost ×3
c++ ×3
c++11 ×2
coroutine ×2
future ×2
yield ×2
ace-editor ×1
autobahn ×1
bespin ×1
boost-asio ×1
c ×1
dtrace ×1
freebsd ×1
javascript ×1
libuv ×1
linux ×1
networking ×1
sockets ×1
sql ×1
sql-server ×1
tcp ×1
twisted ×1