小编Jay*_*K23的帖子

使用 asyncio.run() 时关闭 Asyncio 事件循环

我开始使用 AsyncIO 和 AioHTTP,我正在编写一些基本代码来熟悉语法。我尝试了以下应该同时执行 3 个请求的代码:

import time
import logging
import asyncio
import aiohttp
import json
from aiohttp import ClientSession, ClientResponseError
from aiocfscrape import CloudflareScraper

async def nested(url):
    async with CloudflareScraper() as session:
        async with session.get(url) as resp:
            return await resp.text()

async def main():
    URL = "https://www.binance.com/api/v3/exchangeInfo"
    await asyncio.gather(nested(URL), nested(URL), nested(URL))

asyncio.run(main())
Run Code Online (Sandbox Code Playgroud)

这是输出:

raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我会收到这个错误,有人可以帮我解决这个问题吗?

python python-3.x python-asyncio

9
推荐指数
2
解决办法
715
查看次数

Django rest 框架 - 如何限制对 API 端点的请求?

我使用 Django Rest Framework 创建了一个 API,现在我正在研究一个速率限制系统,以避免垃圾邮件。内置的节流系统效果很好,我设法添加了多个节流:

REST_FRAMEWORK = {
        # 'DEFAULT_AUTHENTICATION_CLASSES': (
        #     "xapi.authentication_backends.TokenBackend",
        # ),
        'DEFAULT_THROTTLE_CLASSES': [
            'rest_framework.throttling.AnonRateThrottle',
            'rest_framework.throttling.UserRateThrottle'
        ],
        'DEFAULT_THROTTLE_RATES': {
            'anon': '70/minute',
            'user': '70/minute',
            'user_sec': '2/second',
            'user_min': '120/minute',
            'user_hour': '7200/hour',
        },
        
        'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        )
    }
Run Code Online (Sandbox Code Playgroud)

而在我的views.py

class UserSecThrottle(UserRateThrottle):
    scope = 'user_sec'

class UserMinThrottle(UserRateThrottle):
    scope = 'user_min'

class UserHourThrottle(UserRateThrottle):
    scope = 'user_hour'
Run Code Online (Sandbox Code Playgroud)

因此,如果某个用户在一分钟内执行超过 120 个查询,该用户将被阻止一分钟,如果违反小时限制,该用户将被阻止一小时。有什么方法可以决定用户被阻止的程度吗?例如,如果某人在一分钟内执行超过 120 次查询,我想阻止某人 10 分钟。任何建议表示赞赏。

python django

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

Nuxt - can i run client side code from a middleware in Universal mode?

I'm working on a universal Nuxt app that uses the standard Django session authentication. I need to restrict some pages in my project to logged in users only, so i decided to use a middleware.

The problem with the middleware is that it will run from server side, so it will always return False to the user even when the user is logged in, since it doesn't send any cookie in the request. This happens only when i refresh page …

javascript vue.js nuxt.js

5
推荐指数
1
解决办法
4543
查看次数

如何在 Python 中平滑数据?

我正在使用 Python 来检测 OHLC 数据上的一些模式。我的问题是我拥有的数据非常嘈杂(我使用的是来自 Open/High/Low/Close 数据集的 Open 数据),并且它经常导致我得到不正确或微弱的结果。

有什么方法可以“平滑”这些数据,或者减少噪音,以改善我的结果?我可以使用哪些算法或库来完成这项任务?

这是我的数据示例,它是一个普通数组:

DataPoints = [6903.79, 6838.04, 6868.57, 6621.25, 7101.99, 7026.78, 7248.6, 7121.4, 6828.98, 6841.36, 7125.12, 7483.96, 7505.0, 7539.03, 7693.1, 7773.51, 7738.58, 8778.58, 8620.0, 8825.67, 8972.58, 8894.15, 8871.92, 9021.36, 9143.4, 9986.3, 9800.02, 9539.1, 8722.77, 8562.04, 8810.99, 9309.35, 9791.97, 9315.96, 9380.81, 9681.11, 9733.93, 9775.13, 9511.43, 9067.51, 9170.0, 9179.01, 8718.14, 8900.35, 8841.0, 9204.07, 9575.87, 9426.6, 9697.72, 9448.27, 10202.71, 9518.02, 9666.32, 9788.14, 9621.17, 9666.85, 9746.99, 9782.0, 9772.44, 9885.22, 9278.88, 9464.96, 9473.34, 9342.1, 9426.05, 9526.97, 9465.13, …
Run Code Online (Sandbox Code Playgroud)

python numpy python-3.x pandas

3
推荐指数
2
解决办法
1700
查看次数

Vue - 设置输入文本的默认值

我正在加载一个 Vue 组件,它应该呈现一个基本表单。我试图将输入设置为默认值,但由于某种原因没有显示任何内容。这是我尝试过的:

<template>
    ...
    <input type="text" value="0.02" class="form-control" v-model="amount">
    ...
</template>
Run Code Online (Sandbox Code Playgroud)

为什么输入字段没有显示任何内容?是因为Vue不支持它还是我需要使用其他东西?提前致谢

input vue.js

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