Recent

Author Topic: Multithreading and connection to firebird.  (Read 2388 times)

abmservice

  • Jr. Member
  • **
  • Posts: 69
Multithreading and connection to firebird.
« on: August 18, 2019, 09:33:31 pm »
Hello.
He is crawling on the subject of multithreading, so please help.
I have an application that reads data from the firebird database every 1 minute. The query returns the average of the last 15 minutes. The table has over 4 million records and within 1 minute about 40-60 new ones are added.
When I read, the program freezes for 2-3 seconds.
I would like to do it in a separate thread not to freeze the application every minute. I am asking for an example of how to implement it.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Multithreading and connection to firebird.
« Reply #1 on: August 18, 2019, 11:27:12 pm »
I am not much of a Db guy but from what you are saying here maybe a stand alone app using some process communications is the best idea, that way your main app does not freeze.
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Multithreading and connection to firebird.
« Reply #2 on: August 19, 2019, 12:27:51 am »
See:
and other examples in the wiki (and in the FPC sources)
« Last Edit: August 19, 2019, 12:30:54 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

abmservice

  • Jr. Member
  • **
  • Posts: 69
Re: Multithreading and connection to firebird.
« Reply #3 on: August 23, 2019, 08:30:47 pm »
Sure, I need to create components dynamically in a thread?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Multithreading and connection to firebird.
« Reply #4 on: August 23, 2019, 09:09:39 pm »
Sure, I need to create components dynamically in a thread?

Not unless strictly needed, and you'll very rarely, if ever, need to create a component inside a thread.

Create (and destroy) components and controls in the main thread (the application). In other threads you can use them, using Synchronize as needed.
« Last Edit: August 23, 2019, 09:11:29 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Multithreading and connection to firebird.
« Reply #5 on: August 24, 2019, 07:09:56 am »
Hello.
He is crawling on the subject of multithreading, so please help.
This subject has been discussed recently:

MultiThread Programming, more Thread not always faster
https://forum.lazarus.freepascal.org/index.php/topic,45775.0.html

[SOLVED] Why does this code crash?
https://forum.lazarus.freepascal.org/index.php/topic,25380.0.html

Quote
I have an application that reads data from the firebird database every 1 minute. The query returns the average of the last 15 minutes. The table has over 4 million records and within 1 minute about 40-60 new ones are added.
When I read, the program freezes for 2-3 seconds.
The problem of low performance in this case lies on the Firebird server, not on your Lazarus program.

Quote
I would like to do it in a separate thread not to freeze the application every minute. I am asking for an example of how to implement it.
See:
Multithreaded Application Tutorial
Manager Worker Threads System
and other examples in the wiki (and in the FPC sources)
+1

abmservice

  • Jr. Member
  • **
  • Posts: 69
Re: Multithreading and connection to firebird.
« Reply #6 on: August 24, 2019, 12:13:35 pm »
Multithreading is new to me. I study hard now and try to understand how it works. Thanks for every tip. Until now, some simple applications more hobby, but you need to develop.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Multithreading and connection to firebird.
« Reply #7 on: August 24, 2019, 02:00:52 pm »
You have started on the deep end, what with mixing multithreading and database access.

I would recommend starting with some very simple program with a button to start a thread and another to terminate it. The thread would do nothing but check if it should end.

With that you can test and learn how to control threads from the main application. From there you go on introducing new features to test/learn some new aspect until you get it all fixed in your mind.

After that, using threads for anything in almost any application should be a snap :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

abmservice

  • Jr. Member
  • **
  • Posts: 69
Re: Multithreading and connection to firebird.
« Reply #8 on: August 24, 2019, 03:03:36 pm »
I agree. I know one thing - to go further I need to understand multithreading.

Fungus

  • Sr. Member
  • ****
  • Posts: 353
Re: Multithreading and connection to firebird.
« Reply #9 on: August 24, 2019, 04:11:53 pm »
The query returns the average of the last 15 minutes.

Instead of doing it like that (using aggregate functions) your program should keep track of the records for the last 15 minutes and calculate the average when needed. That would most likely speed up your DB access times without the need of threads.

incendio

  • Sr. Member
  • ****
  • Posts: 269
Re: Multithreading and connection to firebird.
« Reply #10 on: August 26, 2019, 10:03:40 am »
As valdir.marcos posted, that was a multithreading example with Firebird database.

But keep in mind, your table structure must have index that able to split queries into a small sections. Without this ability, forget about multithreading.

After split into small sections, assign each section to each thread. From my experience, max thread created is about 75% of hardware thread. For ex. if your cpu has 8 threads, # of thread created in Lazarus is 6 (8x75%).

 

TinyPortal © 2005-2018