Skip to content

Instantly share code, notes, and snippets.

@blackrez
Created February 6, 2022 19:39
Show Gist options
  • Save blackrez/d1216dfd8b7f062f071a7bcd4596469f to your computer and use it in GitHub Desktop.
Save blackrez/d1216dfd8b7f062f071a7bcd4596469f to your computer and use it in GitHub Desktop.
duckdb with thread
from multiprocessing import connection
from time import sleep, perf_counter
from threading import Thread
import duckdb
con = duckdb.connect(database=':memory:')
con.execute("CREATE TABLE items(item VARCHAR, value DECIMAL(10,2), count INTEGER)")
con.execute("INSERT INTO items VALUES ('jeans', 20.0, 42), ('hammer', 42.2, 4242)")
def task(a):
print(a)
cursor = con.cursor()
cursor.execute("SELECT * FROM items WHERE count=?", [a])
print(cursor.fetchall())
sleep(1)
print('done')
start_time = perf_counter()
# create two new threads
t1 = Thread(target=task, args=[42])
t2 = Thread(target=task, args=[4242])
# start the threads
t1.start()
t2.start()
# wait for the threads to complete
t1.join()
t2.join()
end_time = perf_counter()
print(f'It took {end_time- start_time: 0.2f} second(s) to complete.')
@blackrez
Copy link
Author

@Raidus, I don't know what changed exactly but I confirm there was some changes between 0.3.4 and 0.4.0 in thread API. I tested on MacOS (M1) and it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment