小编Mon*_*oya的帖子

Python - 实现 Future 对象

我需要 python3 中的 Future 对象。

Python 有两个内置实现:

  1. asyncio 模块有一个,但它不适合我的应用程序。

  2. 并发.futures 有一个,但其文档指出该对象只能由 Executor 对象创建:

以下 Future 方法适用于单元测试和 Executor 实现。

https://docs.python.org/3/library/concurrent.futures.html#future-objects

基本接口是:

class Future:
   def get(self):
       pass
   def set(self, val):
       pass
Run Code Online (Sandbox Code Playgroud)

虽然这里的关键是 ifget在之前调用set,但该方法会阻塞,直到设置值。

set正在设置该值并释放正在等待的任何线程get

在多线程应用程序中使用上述内置实现之一是否安全?

如何高效实施?

python concurrency future concurrent.futures

4
推荐指数
1
解决办法
1939
查看次数

Android - 存储访问框架 - Uri 到本地文件

我正在Storage Access Framework(SAF)我的应用程序中使用。为了扫描文件(照片或文档),我需要通过 API 发送本地文件路径。我已经成功地正确设置了SAF,现在当用户选择一个文件时,我得到了一个应该的 Uri。

看来 Uri 是云的密钥,而不是本地文件。

Uri 值如下所示:

content://com.android.providers.media.documents/document/image:11862 
Run Code Online (Sandbox Code Playgroud)

如何将此 Uri 转换为本地文件?我应该从云端下载文件吗?我该怎么做?

java android storage-access-framework

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

蟒蛇| HTTP - 如何在下载前检查文件大小

我正在使用 urllib3 爬网。示例代码:

from urllib3 import PoolManager

pool = PoolManager()
response = pool.request("GET", url)
Run Code Online (Sandbox Code Playgroud)

问题是我可能会偶然发现 url 是一个非常大的文件的下载,我并不介意下载它。

我发现了这个问题 - Link - 它建议使用urlliband urlopen。我不想两次联系服务器。

我想将文件大小限制为 25MB。有没有办法做到这一点urllib3

python http urllib urllib2 urllib3

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

Kubernetes HPA - 延长冷却时间

我正在运行一个带有 HPA 的 Kubernetes 集群 v1.16(目前 GKE 上的最新版本),它根据自定义指标(特别是从谷歌云监控获取的rabbitmq消息计数)扩展部署。

问题

当消息计数暂时较高时,部署会非常快地扩展到最大 Pod 计数。

信息

HPA --horizo​​ntal-pod-autoscaler-sync-period 在 GKE 上设置为 15 秒,据我所知无法更改。

我的自定义指标每 30 秒更新一次。

我认为导致这种行为的原因是,当队列中每 15 秒就有大量消息计数时,HPA 会触发扩展,并在几个周期后达到最大 pod 容量。

在 kubernetes api v1.18 中,您可以控制扩展稳定时间,但我在 v1.16 中找不到类似的功能。

我的问题

如何让 HPA 逐步扩大规模?

编辑1

我的一项部署的 HPA 示例:

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: my-deployment-hpa
  namespace: production
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-deployment
  minReplicas: 6
  maxReplicas: 100
  metrics:
  - type: External
    external:
      metricName: "custom.googleapis.com|rabbit_mq|v1-compare|messages_count"
      metricSelector:
        matchLabels:
          metric.labels.name: production
      targetValue: 500
  
Run Code Online (Sandbox Code Playgroud)

kubernetes google-kubernetes-engine hpa

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

.net core 6 - 实体框架 - 何时使用 AsEnumerable AsQueryable

背景

几周前,我已将项目从 .net core 2.2 迁移到 .net core 6。在过去的几周里,我发现Entity Framework过去运行良好的通话出现了问题。

问题

以下代码在 .net core 2.2 上运行良好:

using (var context = new MyContext())
{
        return await context.Accounts
                    .AsNoTracking()
                    .Where(account => SOME_CONDITIONS)
                    .ToArrayAsync()
                    .ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)

我们发现现在这段代码陷入了某种死锁,函数不返回。

建议的解决方案

添加AsAsyncEnumerable修复了问题并且函数非常快地完成执行:

using (var context = new MyContext())
{
        return await context.Accounts
                    .AsNoTracking()
                    .AsAsyncEnumerable()
                    .Where(account => SOME_CONDITIONS)
                    .ToArrayAsync()
                    .ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)

问题

我看到建议使用 AsEnumerable/AsAsyncEnumerable/AsQueryable 在 .net core 6 上使用 EF 进行调用。

我什么时候应该使用AsEnumerable// AsAsyncEnumerableAsQueryable

c# entity-framework .net-core

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

Android-Recycler视图设置项无法滑动

我正在使用RecyclerViewand ItemTouchHelper.SimpleCallback=> onSwiped方法。回收者视图中的某些项目正在进行中,我想在其上禁用滑动。

我该怎么做?

android android-recyclerview

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

使用样式的警报对话框文本颜色

我刚刚在我的应用程序中更新了库版本,现在AlertDialog按钮的文本颜色为白色.在此之前,colorAccent如果我没记错的话,那就是指定它们的属性.我尝试了很多不同的属性,但似乎没有一个属性能够工作.

示例照片 - 在右下方,您可以看到非常隐蔽的按钮:

在此输入图像描述

我当前的警报对话框样式:

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/website_main</item>
    <item name="android:textColor">@color/website_main</item>
</style>
Run Code Online (Sandbox Code Playgroud)

以我的主题风格:

<item name="android:alertDialogStyle">@style/AlertDialogStyle</item>
Run Code Online (Sandbox Code Playgroud)

我正在使用的支持/设计/ appcompat的当前版本是: 25.1.0

我在其中创建的Java代码AlertDialog:

android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(activity);
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton(activity.getString(R.string.ok), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    <UnrelatedLogic...>
                }
            });
    return alertDialogBuilder.show();
Run Code Online (Sandbox Code Playgroud)

在这里我使用的ListPreference还使用了AlertDialog:

android.support.v7.preference.ListPreference lang_preference = (android.support.v7.preference.ListPreference) findPreference("language_chooser");
Run Code Online (Sandbox Code Playgroud)

有什么建议?

android android-alertdialog android-styles

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

Pub\Sub Python 客户端 - 正常关闭订阅者

我在 python3.6 中使用 Google Pub/Sub 客户端 v2.2.0 作为订阅者。

我希望我的应用程序在确认它已经收到的所有消息后正常关闭。

来自 Google 指南的订阅者的示例代码,稍作改动将显示我的问题:

from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
from time import sleep

# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
# Number of seconds the subscriber should listen for messages
# timeout = 5.0

subscriber = pubsub_v1.SubscriberClient()
# The `subscription_path` method creates a fully qualified identifier
# in the form `projects/{project_id}/subscriptions/{subscription_id}`
subscription_path = subscriber.subscription_path(project_id, subscription_id)

def callback(message):
    print(f"Received {message}.")
    sleep(30)
    message.ack()
    print("Acked")

streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback) …
Run Code Online (Sandbox Code Playgroud)

python publish-subscribe google-cloud-platform google-cloud-pubsub pypubsub

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

Kubernetes Ingress - 负载均衡器流量分割

我有一个 kubernetes 类入口nginx和两个负载均衡器。在 GKE v1.17 上运行。

入口 yaml 示例:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
    kubernetes.io/ingress.class: "nginx"
    # Enable client certificate authentication
    nginx.ingress.kubernetes.io/auth-tls-verify-client: "on"
    # Create the secret containing the trusted ca certificates
    nginx.ingress.kubernetes.io/auth-tls-secret: "production/client-cert-secret"
    # Specify the verification depth in the client certificates chain
    nginx.ingress.kubernetes.io/auth-tls-verify-depth: "1"
    # Automatically redirect http to https
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    # Use regex in paths
    nginx.ingress.kubernetes.io/use-regex: "true"
    # Allow larger request body
    nginx.ingress.kubernetes.io/proxy-body-size: 30m
    # For notifications …
Run Code Online (Sandbox Code Playgroud)

nginx kubernetes kubernetes-ingress nginx-ingress

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