我有一个python多线程应用程序.我想在一个线程中运行一个asyncio循环,并从另一个线程发送回调和协同程序.应该很容易,但我无法理解asyncio的东西.
我提出了以下解决方案,它可以完成我想要的一半,随意评论任何事情:
import asyncio
from threading import Thread
class B(Thread):
def __init__(self):
Thread.__init__(self)
self.loop = None
def run(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop) #why do I need that??
self.loop.run_forever()
def stop(self):
self.loop.call_soon_threadsafe(self.loop.stop)
def add_task(self, coro):
"""this method should return a task object, that I
can cancel, not a handle"""
f = functools.partial(self.loop.create_task, coro)
return self.loop.call_soon_threadsafe(f)
def cancel_task(self, xx):
#no idea
@asyncio.coroutine
def test():
while True:
print("running")
yield from asyncio.sleep(1)
b.start()
time.sleep(1) #need to wait for loop to start
t …Run Code Online (Sandbox Code Playgroud) 我参与了一个开源的c ++应用程序.在该应用程序中,所有日志记录都使用诸如
if (Debug) std::cout << "MyClass | my debug message" << MyExpensiveStringConvertion() << std::endl;
Run Code Online (Sandbox Code Playgroud)
用于使用更高级的日志记录框架(在其他语言中)我建议使用一个现有的框架,如boost,easylogginppp,无论是自动格式化还是可以在运行时配置.
但我回答说使用"if(Debug)"在写入时几乎没有开销
Log(debug) << "MyClass | my debug message" << MyExpensiveStringConvertion()
Run Code Online (Sandbox Code Playgroud)
在禁用日志记录时,需要计算<<运算符和MyExpensiveStringConvertion()事件.这个论点是否正确?如果它是正确的,我们应该关心吗?看起来大多数日志框架都以这种方式工作,显然大多数开发人员都不关心
更新:我理解如果(Debug)表单只需要一个简单的bool测试,但我期望日志框架实现各种技巧以降低成本,如"#define LOG(level)if(doDebug(level))Log(level) "DietmarKühl提到.
我正在查看 pythonlogging模块来替换自定义代码,但我无法决定应该在哪里调用该basicConfig方法。
如果我开发一个名为 其具有子模块的模块,那么只在 中调用一次mymodule似乎是个好主意。basicConfigmymodule
但是,如果我(或其他人)想要mymodule在自己的模块或程序中使用并使用该logging模块,他们就无法调用自己,并且只能使用in 中basicConfig提供的选项。basicConfigmymodule
我知道有一些技巧可以解决这个问题。但是正确的使用方法是什么basicConfig,应该在哪里调用呢?