multithreading & sqlite
monkyyy
crazymonkyyy at gmail.com
Tue Jan 27 20:15:53 UTC 2026
On Tuesday, 27 January 2026 at 16:38:08 UTC, Lars Johansson wrote:
> I have created a program using multithreading and Sqlite3.
> This is a prestudy for map&reduce.
>
> I found some parts of the program very hard to digest
> especially the 'as!' crap.
> Sqlite was not simple (for me) to set up and use. It took some
> time to understand I must make a physical copy of the result
> (the .idup).
> It was hard to make this work.
>
> I would appreciate, if anyone can comment on things I can do
> better:
> (On your own, it not easy to know what can be done better)
>
> import std.parallelism;
> import std.stdio;
> import core.thread;
> import std.array;
> import d2sqlite3;
>
> void main() {
> writeln("starting");
> auto myresults = getData("mydata.db");
> auto pool = new TaskPool(2); // Create pool with only 2
> worker threads
> foreach (i,row; pool.parallel(myresults)) {
> writeln(row);
> auto ID = row[0];
> auto Name = row[1];
> auto Age= row[2];
> writeln("Starting task ",i, " ",ID, " ",Name, " ", Age);
> Thread.sleep(1.seconds); // Simulate work
> writeln("finish task ",i);
> }
> pool.finish();
> }
> string[][] getData(string DB){
> string[][] results;
> writeln("getData starts");
> auto db = Database(DB);
> foreach (row; db.execute("SELECT * FROM users")) {
> writeln("ID: ", row[0].as!int); //WTF is this
> as! crap
> writeln("Name: ", row[1].as!string);
> writeln("Age: ", row[2].as!int);
> results ~= [
> row[0].as!string.idup, // .idup makes
> immutable copy
> row[1].as!string.idup, // why do i need
> this?
> row[2].as!string.idup // and why do I
> have to convert to string?
> ];}
> writeln("getData done");
> return results;
> }
`!` is a template call, and is always a template call; c syntax
doesnt allow you to pass a type to a function 1 char and the
double argument list was considered the best way forward.
I dont know where as comes from, but dup is used when you get a
once off copy to a slice, `string` is *just* a one line of code
`alias string=immutable char[]` in object. This make it a
reference type.
---
I dont like the api's people make for deserialization, this looks
like a bad one. Look for something that parses an entire "row" as
a struct that way your passing bundles of types already.
More information about the Digitalmars-d-learn
mailing list