如何使用Boto3客户端删除仍可用的HIT

时间:2019-01-15 12:17:45

标签: python boto3 mechanicalturk

我为工人提供了一些已发布的HIT。现在,我想删除它们,尽管它们尚未由工人完成。根据此文档,不可能:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/mturk.html#MTurk.Client.delete_hit

只有处于可审核状态的HIT才能删除。

但是使用命令行界面似乎可行:https://docs.aws.amazon.com/AWSMechTurk/latest/AWSMturkCLT/CLTReference_DeleteHITsCommand.html

我的问题是,我可以通过boto3客户端以某种方式完成删除不可审查的HIT的命令行行为吗?

1 个答案:

答案 0 :(得分:2)

部分解决方案是将“可分配” HIT设置为立即失效。我使用以下脚本清理Mechanical Turk沙箱:

import boto3
from datetime import datetime

# Get the MTurk client
mturk=boto3.client('mturk',
        aws_access_key_id="aws_access_key_id",
        aws_secret_access_key="aws_secret_access_key",
        region_name='us-east-1',
        endpoint_url="https://mturk-requester-sandbox.us-east-1.amazonaws.com",
    )

# Delete HITs
for item in mturk.list_hits()['HITs']:
    hit_id=item['HITId']
    print('HITId:', hit_id)

    # Get HIT status
    status=mturk.get_hit(HITId=hit_id)['HIT']['HITStatus']
    print('HITStatus:', status)

    # If HIT is active then set it to expire immediately
    if status=='Assignable':
        response = mturk.update_expiration_for_hit(
            HITId=hit_id,
            ExpireAt=datetime(2015, 1, 1)
        )        

    # Delete the HIT
    try:
        mturk.delete_hit(HITId=hit_id)
    except:
        print('Not deleted')
    else:
        print('Deleted')