#!/usr/bin/env python
import sqlite3
import concurrent.futures

"""
Template for multithreading processing.
Leverage a SQLITE DB to gather data.

Dependencies: None
"""

# Constants
DB_FILE = "data.db"

# Worker handling work on a item
def worker(item_id):
    print(f"\rWorking on item: {item_id}", end="", flush=True)
    # Do processing to get data
    data = "....."
    with sqlite3.connect(DB_FILE) as connection:
        curs = connection.cursor()
        curs.execute("INSERT INTO result (data) VALUES(?);",(str(data),))


def main(thread_count):
    print("[+] Initialize DB...")
    with sqlite3.connect(DB_FILE) as connection:
        curs = connection.cursor()
        curs.execute("CREATE TABLE IF NOT EXISTS result (id integer PRIMARY KEY, data text NOT NULL);")
        curs.execute("DELETE FROM result;")    
    print(f"[+] Start work using {thread_count} workers in parallel...")
    with concurrent.futures.ThreadPoolExecutor(max_workers=thread_count) as executor:
        for i in range(0, 100):
            executor.submit(worker, item_id=i)    
    print(f"\n[+] Data gathered: ")
    with sqlite3.connect(DB_FILE) as connection:
        curs = connection.cursor()
        curs.execute("SELECT data FROM result;")
        for row in curs.fetchall():
            print(row[0])


if __name__ == "__main__":
    thread_count = 20
    main(thread_count)
