Created
February 6, 2022 19:39
-
-
Save blackrez/d1216dfd8b7f062f071a7bcd4596469f to your computer and use it in GitHub Desktop.
duckdb with thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') |
@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
Did something changed in the duckdb API?
I am getting the error with the exact same code
I am using