小编luc*_*fer的帖子

如何存储线程的JoinHandle以便稍后关闭它

我试图在后台运行一个线程,然后更改 AtomicBool 来要求线程停止。为了确保线程正确停止,我想调用joinJoinHandle 的方法。

thread::sleep_ms如果我在设置 AtomicBool 后等待 () 一段时间,而不是调用 join ,线程就会正确关闭。但为了确保这一点,我想使用 join。或者有没有更好的方法来确保线程正确关闭?

use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::thread::JoinHandle;

struct MyInner { s: String }

struct My {
    inner: Arc<Mutex<MyInner>>,
    close_thread: Arc<AtomicBool>,
    my_thread: JoinHandle<()>
}

impl MyInner {
    fn new(s: String) -> MyInner {
        MyInner {
            s: s
        }
    }
}

impl My {
    fn new(s: String) -> My {
        My {
            inner: Arc::new(Mutex::new(MyInner::new(s))),
            close_thread: Arc::new(AtomicBool::new(false)),
            my_thread: thread::spawn(move || {})
        }
    }

    fn …
Run Code Online (Sandbox Code Playgroud)

rust

13
推荐指数
0
解决办法
6150
查看次数

delete_message_batch 并没有真正从 SQS 队列中删除消息

我正在使用标准的 Amazon SQS 队列。在python3中使用boto3库与SQS交互。以下是我接收消息然后删除它们的代码:

from boto3.session import Session
boto3_session = Session(region_name=SQS_REGION_NAME, aws_access_key_id=SQS_ACCESS_ID,
                                aws_secret_access_key=SQS_ACCESS_KEY)
sqs = boto3_session.client('sqs')

response = sqs.receive_message(
    MessageAttributeNames=[
        'EventToReport',
    ],
    QueueUrl=queue_url,
    MaxNumberOfMessages=10,
    VisibilityTimeout=0,
    WaitTimeSeconds=0
)
messages = response['Messages']
receipt_handles = [{'Id': str(index), 'ReceiptHandle': msg['ReceiptHandle']} for index, msg in enumerate(messages)]
sqs.delete_message_batch(QueueUrl=queue_url, Entries=receipt_handles)
Run Code Online (Sandbox Code Playgroud)

这将返回一个成功响应:

{'Successful': [{'Id': '0'}, {'Id': '1'}, {'Id': '2'}, {'Id': '3'}, {'Id': '4'}, {'Id': '5'}, {'Id': '6'}, {'Id': '7'}, {'Id': '8'}, {'Id': '9'}], 'ResponseMetadata': {'RequestId': 'bb28855b-6522-5a1e-a649-d7b3fdabfebe', 'RetryAttempts': 0, 'HTTPStatusCode': 200, 'HTTPHeaders': {'content-length': '1008', 'connection': 'keep-alive', 'server': 'Server', 'date': …
Run Code Online (Sandbox Code Playgroud)

python messaging amazon-sqs boto3

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

标签 统计

amazon-sqs ×1

boto3 ×1

messaging ×1

python ×1

rust ×1