小编Gau*_*ani的帖子

在协同链接期间,await如何将控制权交还给事件循环?

我正在尝试使用Python 3.6中的asyncio,并且很难弄清楚为什么这段代码的行为方式如此.

示例代码:

import asyncio

async def compute_sum(x, y):
    print("Compute %s + %s ..." % (x, y))
    await asyncio.sleep(5)
    print("Returning sum")
    return x + y

async def compute_product(x, y):
    print("Compute %s x %s ..." % (x, y))
    print("Returning product")
    return x * y

async def print_computation(x, y):
    result_sum = await compute_sum(x, y)
    result_product = await compute_product(x, y)
    print("%s + %s = %s" % (x, y, result_sum))
    print("%s * %s = %s" % (x, y, result_product))

loop = asyncio.get_event_loop()
loop.run_until_complete(print_computation(1, 2)) …
Run Code Online (Sandbox Code Playgroud)

python python-3.x async-await python-asyncio

8
推荐指数
1
解决办法
635
查看次数

使用现有S3存储桶触发Lambda函数时如何避免授予`iam:CreateRole`权限?

我正在尝试部署一个 AWS Lambda 函数,当 AVRO 文件写入现有 S3 存储桶时,该函数会被触发。

我的serverless.yml配置如下:

service: braze-lambdas

provider:
  name: aws
  runtime: python3.7
  region: us-west-1
  role: arn:aws:iam::<account_id>:role/<role_name>
  stage: dev
  deploymentBucket:
    name: serverless-framework-dev-us-west-1
    serverSideEncryption: AES256

functions:
  hello:
    handler: handler.hello
    events:
      - s3:
          bucket: <company>-dev-ec2-us-west-2
          existing: true
          events: s3:ObjectCreated:*
          rules:
            - prefix: gaurav/lambdas/123/
            - suffix: .avro
Run Code Online (Sandbox Code Playgroud)

当我运行时serverless deploy,出现以下错误:

ServerlessError: An error occurred: IamRoleCustomResourcesLambdaExecution - API: iam:CreateRole User: arn:aws:sts::<account_id>:assumed-role/serverless-framework-dev/jenkins_braze_lambdas_deploy is not authorized to perform: iam:CreateRole on resource: arn:aws:iam::<account_id>:role/braze-lambdas-dev-IamRoleCustomResourcesLambdaExec-1M5QQI6P2ZYUH. 
Run Code Online (Sandbox Code Playgroud)

由于 CloudFormation 的工作原理,我看到一些提到需要无服务器,iam:CreateRole但如果我想使用,谁能确认这是否是唯一的解决方案existing: true?除了使用框架添加对配置的支持之前使用的旧无服务器插件之外,还有其他方法吗 …

amazon-iam serverless-framework serverless-plugins aws-serverless

5
推荐指数
2
解决办法
3963
查看次数

根据 SQLAlchemy 中的其他列计算列的值?

我在 Python 代码库中使用 SQLAlchemy 作为 ORM,以便能够与 MySQL 数据库连接。我希望能够根据同一行其他列中的值自动计算并在列中插入一个值。

这就是我的班级的样子:

class SentNotification(Base):
    __tablename__ = 'sent_notification'

    campaign = Column(VARCHAR(34), nullable=False)
    alert_type = Column(VARCHAR(34), nullable=False)
    identifier = Column(VARCHAR(34), nullable=False)
    message_params = Column(JSON, nullable=True)
    send_time = Column(Integer, nullable=False)
    nonce = Column(VARCHAR(32), nullable=False, primary_key=True)
Run Code Online (Sandbox Code Playgroud)

nonce列应该是campaignalert_type和(json 转储) 的 md5 哈希(identifier或任何其他自定义函数) message_params

显然,我可以单独计算它并使用 进行调用SentNotificationnonce但如果我只能传递前 5 个值,并且我的类将自动处理 的计算和插入,那么它会更干净nonce

注意:我不想使用混合属性,因为它会计算值,并且我想实际将其保留nonce在数据库中。

python mysql sqlalchemy python-3.x

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