我有一个long_task运行大量cpu绑定计算的函数,我希望通过使用新的asyncio框架使其异步.生成的long_task_async函数使用a ProcessPoolExecutor将工作卸载到不同的进程,不受GIL约束.
麻烦的是,由于某种原因,concurrent.futures.Future实例ProcessPoolExecutor.submit从投掷时返回a TypeError.这是设计的吗?那些期货与asyncio.Future班级不兼容吗?什么是解决方法?
我也注意到发电机不是可拣选的,所以提交一个couroutine ProcessPoolExecutor就会失败.这个也有任何清洁的解决方案吗?
import asyncio
from concurrent.futures import ProcessPoolExecutor
@asyncio.coroutine
def long_task():
yield from asyncio.sleep(4)
return "completed"
@asyncio.coroutine
def long_task_async():
with ProcessPoolExecutor(1) as ex:
return (yield from ex.submit(long_task)) #TypeError: 'Future' object is not iterable
# long_task is a generator, can't be pickled
loop = asyncio.get_event_loop()
@asyncio.coroutine
def main():
n = yield from long_task_async()
print( n )
loop.run_until_complete(main())
Run Code Online (Sandbox Code Playgroud) 我正在使用Qt 4.8和BB10.
我为要实现的类定义了一个基本接口:
class AbstractImageProcessor : public QObject
{
public:
AbstractImageProcessor(QObject * parent) : QObject(parent) {}
virtual QImage process(const QByteArray &data) = 0;
virtual ~AbstractImageProcessor(){ }
};
Run Code Online (Sandbox Code Playgroud)
我希望从QML中使用的一个这样的实现看起来像这样:
class WebImageProcessor : public AbstractImageProcessor
{
Q_OBJECT
Q_PROPERTY(int maxHeight READ getMaxHeight WRITE setMaxHeight NOTIFY maxHeightChanged)
Q_PROPERTY(int maxWidth READ getMaxWidth WRITE setMaxWidth NOTIFY maxWidthChanged)
Q_PROPERTY(bool fit READ isFit NOTIFY fitChanged)
public WebImageProcessor(QObject * parent = 0) : AbstractImageProcessor(parent) {}
virtual ~WebImageProcessor() {}
/* rest of code omitted */
};
Run Code Online (Sandbox Code Playgroud)
我想将此AbstractImageProcessor作为另一个QML类型的属性公开:
class …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚如何从C调用泛型方法(静态和非静态).
最大的罪魁祸首是调用泛型方法基本上没有文档记录,单声道存储库中没有示例,但有人提到这可能在文档中是可能的:
If you want to invoke generic methods, you must call the method on the "inflated" class, which you can obtain from the mono_object_get_class()
MonoClass *clazz;
MonoMethod *method;
clazz = mono_object_get_class (obj);
/*
* If there are more Add methods declared, you
* may use mono_method_desc_search_in_class (clazz, ":Add(T)"),
* you must substitute ":Add(T)" with the correct type, for example
* for List<int>, you would use ":Add(int)".
*/
method = mono_class_get_method_from_name (clazz, "Add", 1);
mono_runtime_invoke (method, obj, args, &exception);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这并没有那么有用,因为它没有显示完整的示例,无论我做什么(使用 …