Page 1 of 1

FCGI threading?

Posted: Wed Jun 08, 2016 11:35 am
by Tuireann
Is it possible to spawn FastCGI worker threads? I've been working on a FastCGI program for a couple weeks. I assumed that like the C FastCGI library you could start a worker thread to sit on WaitFastCGIRequest(). I spawn several worker threads but when the server is under heavy load(from "ab -n 10000 -c 32 http://localhost/") top/htop show that only one of the threads is ever experiencing any load(always the same thread). This behaviour is identical under both apache and nginx with both PureBasic 5.42 and 5.50b1 under OpenSuse 42.1 64bit and Ubuntu 15.10 64bit so I was curious if it was just a limitation of the library or a bug.

Re: FCGI threading?

Posted: Wed Jun 08, 2016 2:53 pm
by bbanelli
Tuireann wrote:Is it possible to spawn FastCGI worker threads? I've been working on a FastCGI program for a couple weeks. I assumed that like the C FastCGI library you could start a worker thread to sit on WaitFastCGIRequest(). I spawn several worker threads but when the server is under heavy load(from "ab -n 10000 -c 32 http://localhost/") top/htop show that only one of the threads is ever experiencing any load(always the same thread). This behaviour is identical under both apache and nginx with both PureBasic 5.42 and 5.50b1 under OpenSuse 42.1 64bit and Ubuntu 15.10 64bit so I was curious if it was just a limitation of the library or a bug.
Have you enabled threaded flag in PB settings?

Re: FCGI threading?

Posted: Wed Jun 08, 2016 3:15 pm
by Tuireann
Yes, I am compiling with the -t flag. If I don't it crashes which made me wonder if the lack of thread utilisation was a bug.

Re: FCGI threading?

Posted: Wed Jun 08, 2016 3:24 pm
by Fred
It should work, the lib is built using threading on. I've seen this thread: http://stackoverflow.com/questions/1866 ... ithreading. Could you try to put

Code: Select all

umask_(0)
before calling InitFastCGI() and see if it does a difference ?

Re: FCGI threading?

Posted: Wed Jun 08, 2016 5:10 pm
by Tuireann
Tried adding umask_(0) both before and after InitFastCGI() and neither yielded any result. Still only one thread showing any load.

Re: FCGI threading?

Posted: Wed Jun 08, 2016 5:31 pm
by Tuireann
Are the mutexes in PureBasic reentrant? I found a bug that I must have introduced when i switched from SQLite to PGSQL when I removed all my SQL mutexes and there was a double lock at the start of each thread but one thread would always survive to handle the requests... I wasn't looking for that initially because I would have thought it would have fully stopped the process. But it would appear the error was mine. Sorry! :oops:

Re: FCGI threading?

Posted: Wed Jun 08, 2016 5:44 pm
by Tuireann
I thought I would share in case anyone ever has this problem so they know what to look for so they don't embarrass themself like I did :P

This illustrates what my problem was:

Code: Select all

Global Mutex = CreateMutex()

Procedure.i FCGIWorker(*Parameter)
   
   While WaitFastCGIRequest()
      
      LockMutex(Mutex) ; Lock all other threads out of the mutex!
      LockMutex(Mutex) ; Will not lock the thread that originally locked this mutex!
      
      If ReadCGI()
         
         WriteCGIHeader(#PB_CGI_HeaderContentType, "text/html", #PB_CGI_LastHeader)
         
         WriteCGIString("hello world!")
         Delay(10) ; make some busy thread time
         
         FinishFastCGIRequest()
         
      EndIf
      
      
   Wend
   
EndProcedure


If Not InitCGI()
   End
EndIf

If Not InitFastCGI(5600)
   End
EndIf

CreateThread(@FCGIWorker(), 0)

CreateThread(@FCGIWorker(), 0)

CreateThread(@FCGIWorker(), 0)

FCGIWorker(0)