小编A. *_*vis的帖子

如何在CPython中实现生成器和协同程序?

我已经在CPython中读到过,解释器堆栈(调用到达这一点的Python函数列表)与C堆栈(在解释器自己的代码中调用的C函数列表)混合在一起.如果是这样,那么生成器和协同程序是如何实现的呢?他们如何记住自己的执行状态?CPython是否将每个生成器/ coroutine的堆栈复制到OS堆栈中?或者CPython只是将生成器的最顶层堆栈帧保留在堆上,因为生成器只能从最顶层的帧生成?

python coroutine

47
推荐指数
2
解决办法
4843
查看次数

std:string和std :: string之间的区别

在我的代码的一个功能中,我发现了一个错误.它写道std:string:

const std::string currentDateTime() {
    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    //strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
    strftime(buf, sizeof(buf), "%Y%m%d%X", &tstruct);

    std:string str = buf;

    str.erase(std::remove(str.begin(), str.end(), ':'), str.end());
    return str;
}
Run Code Online (Sandbox Code Playgroud)

代码编译没有错误.为什么编译?什么std:string意思呢?

c++ visual-c++

21
推荐指数
3
解决办法
1757
查看次数

如何检测Gevent的猴子补丁是否有效?

我有一个依赖的Python单元测试multiprocessing,因此当Gevent的猴子补丁处于活动状态时,它不能运行.是否有Python语句可以告诉我是否gevent.monkey.patch_all运行?

python gevent

15
推荐指数
3
解决办法
3380
查看次数

将Python 3 ResourceWarnings转换为异常

有没有办法强制Python 3 unittest失败,而不是简单地向stderr发出警告,如果它导致任何ResourceWarning?

我尝试过以下方法:

import warnings
warnings.simplefilter(action='error', category=ResourceWarning)
Run Code Online (Sandbox Code Playgroud)

这导致unittest的输出:

my_test (__main__.MyTest) ... Exception ignored in: <socket.socket fd=9, family=AddressFamily.AF_INET, type=SocketType.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 54065), raddr=('127.0.0.1', 27017)>
ResourceWarning: unclosed <socket.socket fd=9, family=AddressFamily.AF_INET, type=SocketType.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 54065), raddr=('127.0.0.1', 27017)>
ok

----------------------------------------------------------------------
Ran 1 test in 0.110s
Run Code Online (Sandbox Code Playgroud)

请注意"忽略异常"消息.我宁愿测试失败,而不是要求我阅读其输出,寻找ResourceWarnings.

python python-3.x

7
推荐指数
2
解决办法
1170
查看次数

如何使用"python setup.py nosetests"指定要运行的测试

看,我的setup.py:

https://github.com/mongodb/motor/blob/master/setup.py

...和setup.cfg:

https://github.com/mongodb/motor/blob/master/setup.cfg

我希望能够运行一个套件,例如:

python setup.py nosetests test.test_motor_ssl
Run Code Online (Sandbox Code Playgroud)

但我得到"无效的命令名称'test.test_motor_ssl'".有了这个,另一方面:

python setup.py nosetests --tests test.test_motor_ssl
Run Code Online (Sandbox Code Playgroud)

... nosetests在我的项目中运行每个测试.如何在setup.py中运行测试时如何运行测试的子集?

python nose

6
推荐指数
1
解决办法
3755
查看次数

运行时错误:任务附加到不同的循环

嗨,我正在使用 AsyncIOMotorClient 对 mongoDb 进行异步数据库调用。下面是我的代码。

xyz.py
async def insertMany(self,collection_name,documents_to_insert):
    try:
        collection=self.database[collection_name]
        document_inserted = await collection.insert_many(documents_to_insert)
        return document_inserted
    except Exception:
        raise

def insertManyFn(self,collection_name,documents_to_insert):
    try:
        loop=asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        loop1=asyncio.get_event_loop()
        inserted_documents_count = loop1.run_until_complete(self.insertMany(collection_name, documents_to_insert))
        if inserted_documents_count==len(documents_to_insert):
            document_to_insert={Config.DB_JOB_COLUMN:Job.job_id,Config.DB_JOB_RESULT_COLUMN:Config.DB_JOB_RESULT_SUCCESS}
            loop1.run_until_complete(self.insertOne(Config.DB_JOB_COLLECTION, document_to_insert))
    except Exception:
        raise

xyz1.py
t=Timer(10,xyz.insertManyFn,\
                (collection_name,documents_to_insert))
t.start()   
Run Code Online (Sandbox Code Playgroud)

运行此程序时出现异常

RuntimeError: Task <Task pending coro=<xyz.insertMany() running at <my workspace location>/xyz.py:144> cb=[_run_until_complete_cb() at /usr/lib64/python3.5/asyncio/base_events.py:164]> got Future <Future pending cb=[_chain_future.<locals>._call_check_cancel() at /usr/lib64/python3.5/asyncio/futures.py:431]> attached to a different loop
Run Code Online (Sandbox Code Playgroud)

在上面的程序中,insertManyFn 将在 10 秒后被调用并进行插入操作。但是当它第一次调用 insertMany 时,我得到了一个异常。

mongodb python-asyncio tornado-motor

6
推荐指数
2
解决办法
1万
查看次数

寻找一种从Python代码中分割MongoDB集合的方法

我正在寻找一种在python(2.7)程序中远程执行现有集合的分片的方法.我无法找到执行该功能的API(pymongo),或者可能只是看起来不够好.

这样的事可能吗?

提前致谢

python sharding mongodb pymongo python-2.7

3
推荐指数
1
解决办法
3416
查看次数

如何为 brew 安装的 python 安装 python 包

我刚刚通过 Homebrew 安装了最新的稳定版 python。

$ brew install python3

一切正常。我想安装软件包,例如 PyMongo。我没有点子。

$ pip -bash: pip: 找不到命令

并且没有针对它的 Homebrew 公式:

$ brew 安装 PyMongo

brew install PyMongo 错误:没有可用的 pymongo 公式 搜索公式...正在搜索水龙头...

当通过 Homebrew 安装 Python 时,知道在 OS X 上安装 PyMongo 的最佳方法是什么。谢谢!

python homebrew

3
推荐指数
1
解决办法
9097
查看次数