我试图用以下代码标记未来的超时完成:
import asyncio
@asyncio.coroutine
def greet():
while True:
print('Hello World')
yield from asyncio.sleep(1)
@asyncio.coroutine
def main():
future = asyncio.async(greet())
loop.call_later(3, lambda: future.set_result(True))
yield from future
print('Ready')
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)
“计时器”loop.call_later 将结果设置为 3 秒后的未来。它有效,但我也遇到异常:
Hello World
Hello World
Hello World
Ready
Exception in callback <bound method Task._wakeup of Task(<greet>)<result=True>>(Future<result=None>,)
handle: Handle(<bound method Task._wakeup of Task(<greet>)<result=True>>, (Future<result=None>,))
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\asyncio\events.py", line 39, in _run
self._callback(*self._args)
File "C:\Python33\lib\site-packages\asyncio\tasks.py", line 337, in _wakeup
self._step(value, None)
File "C:\Python33\lib\site-packages\asyncio\tasks.py", line 267, …Run Code Online (Sandbox Code Playgroud) 我的PyCharm不会将任何导入标记为未使用。
我(os未使用):

应该是(在网上找到这张照片):

如何启用它?
基于生成器的协程可以轻松地从函数对象创建:
coro = asyncio.coroutine(func)
Run Code Online (Sandbox Code Playgroud)
原生协程(通常由)怎么样async def?
有什么方法可以从现有的函数对象创建它们吗?
当我使用Python 3.4时,我使用MinGW来编译模块.不幸的是,3.5 MinGW支持不再有效.我已经安装了正确的Visual C++,但pip仍尝试使用MinGW编译器并失败.
如何告诉它使用正确的编译器?
async def start(channel):
while True:
m = await client.send_message(channel, "Generating... ")
generator.makeFile()
with open('tmp.png', 'rb') as f:
await client.send_file(channel, f)
await client.delete_message(m)
await asyncio.sleep(2)
Run Code Online (Sandbox Code Playgroud)
我有一个discord bot,每2秒运行一次任务.我尝试使用无限循环,但是脚本崩溃了,Task was destroyed but it is still pending!我已经阅读了有关asyncio的协同程序的内容,但是我发现await它们都没有使用.await例如,通过运行协同程序可以避免此错误吗?
读取1 GB文件的最佳方法是在其中记录时间序列数据,并生成包含两列(一次和其他数字)的实时图表?我看到你有不同的方法来拖尾文件.
.val() == ""如果textarea在提交之前是空的,我可以使用jQuery 进行简单的检查.它有效,但它会占用空白.
如何在此检查中忽略空格?
// Post guestbook; Check on frontend before submitting
function postGuestbook() {
if ($('.post-form textarea').val() == "")
{
$('div.message.error').show();
return false
}
else {
$('.post-form input[type="submit"]').attr('disabled', 'disabled');
return true;
}
}
Run Code Online (Sandbox Code Playgroud) 有没有可能为类中的所有函数捕获"函数调用之前/之后"事件,而不修饰这些函数中的每一个?可能是一些班装饰者?换句话说,对于这样的代码,我想得到以下输出:
class Foo:
def func1():
print('1')
def func2():
print('2')
c = Foo()
c.func1()
c.func2()
# Output I would like to get:
# func1 called
# 1
# func1 finished
# func2 called
# 2
# func2 finished
Run Code Online (Sandbox Code Playgroud)
我不需要跟踪它.在使用异步函数的类中,我需要知道在其他函数完成之前是否调用了某个函数.
这是取消任务的示例:
import asyncio
async def some_func():
await asyncio.sleep(2)
print('Haha! Task keeps running!')
await asyncio.sleep(2)
async def cancel(task):
await asyncio.sleep(1)
task.cancel()
async def main():
func_task = asyncio.ensure_future(some_func())
cancel_task = asyncio.ensure_future(cancel(func_task))
try:
await func_task
except asyncio.CancelledError:
print('Task cancelled as expected')
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
# Task cancelled as expected
# [Finished in 1.2s]
Run Code Online (Sandbox Code Playgroud)
它工作正常,任务被取消。如果CancelledError被捕获到内部some_func任务不会被取消:
async def some_func():
try:
await asyncio.sleep(2)
except:
pass
print('Haha! Task keeps running!')
await asyncio.sleep(2)
# Haha! Task keeps running!
# …Run Code Online (Sandbox Code Playgroud) python ×6
python-3.x ×5
decorator ×1
discord.py ×1
graph ×1
jquery ×1
matplotlib ×1
pycharm ×1
python-3.5 ×1
tail ×1
textarea ×1
validation ×1
whitespace ×1
windows ×1