Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multithread scan table spend so much time #1258

Open
gangchzu opened this issue Sep 18, 2024 · 0 comments
Open

multithread scan table spend so much time #1258

gangchzu opened this issue Sep 18, 2024 · 0 comments

Comments

@gangchzu
Copy link

my code is below:
from concurrent.futures import thread
from typing import List
from pynamodb.attributes import UnicodeAttribute, NumberAttribute
from pynamodb.models import Model

import time
import threading

class WorkBaseModel(Model):
instance_id = UnicodeAttribute(default="", hash_key=True)
worker_id = UnicodeAttribute(default="", range_key=True)
version = UnicodeAttribute(default="0")
region_name = UnicodeAttribute(default="")
instance_state = UnicodeAttribute(default="")
work_state = NumberAttribute(default=0)
last_report_time = NumberAttribute(default=0)
create_time = NumberAttribute(default=0)
protected = NumberAttribute(default=0)
instance_type = UnicodeAttribute(default="")

class MetaBase:
aws_access_key_id = ""
aws_secret_access_key = ""
max_retry_attempts = 6
max_pool_connections = 40

class ConferenceAssistantInstance(Model):
instance_id = UnicodeAttribute(default="", hash_key=True)
instance_state = UnicodeAttribute(default="")
create_time = NumberAttribute()
protected = NumberAttribute(default=0)
region_name = UnicodeAttribute(default="")

class Meta(MetaBase):
    table_name = "conference_assistant_instance"
    region = "ap-northeast-1"

if name == "main":

def run(i):
    now = time.time()
    all_instance_list: List[ConferenceAssistantInstance] = list(
        ConferenceAssistantInstance.scan()
    )
    print(len(all_instance_list))
    print(f"user time: {i}", time.time()-now)

threads = []
for i in range(40):
    t = threading.Thread(target=run, args=(i,))
    threads.append(t)
    t.start()
    
for thread in threads:
    thread.join()
    
print('over')

I have 100 items in table conference_assistant_instance, when I run this script, spend time is about 3 seconds, but when I replace scan by boto3, it is just 100 milliseconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant