FCGI threading?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Tuireann
User
User
Posts: 10
Joined: Wed Apr 15, 2015 5:42 pm

FCGI threading?

Post 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.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: FCGI threading?

Post 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?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
Tuireann
User
User
Posts: 10
Joined: Wed Apr 15, 2015 5:42 pm

Re: FCGI threading?

Post 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.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: FCGI threading?

Post 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 ?
User avatar
Tuireann
User
User
Posts: 10
Joined: Wed Apr 15, 2015 5:42 pm

Re: FCGI threading?

Post by Tuireann »

Tried adding umask_(0) both before and after InitFastCGI() and neither yielded any result. Still only one thread showing any load.
User avatar
Tuireann
User
User
Posts: 10
Joined: Wed Apr 15, 2015 5:42 pm

Re: FCGI threading?

Post 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:
User avatar
Tuireann
User
User
Posts: 10
Joined: Wed Apr 15, 2015 5:42 pm

Re: FCGI threading?

Post 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)
Post Reply