It is currently Mon Jan 18, 2021 1:34 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: HTTPRequest() not threadsafe?
PostPosted: Tue Mar 17, 2020 9:11 am 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 08, 2004 10:29 am
Posts: 282
Location: Zurich, Switzerland
PB 5.72 LTS Beta 2 (x64) / macOS AND Windows

Maybe you have to run the code multiple times, but here (macOS and Windows) sooner or later the errors occur...

Code:
InitNetwork()

Procedure TestThread(Nr)
   Protected Response$ = ""
   Protected HttpRequest = HTTPRequest(#PB_HTTP_Post, "https://www.purebasic.com/", "" + Str(Nr))
   If HTTPRequest
      Response$ = HTTPInfo(HTTPRequest, #PB_HTTP_Response)
      FinishHTTP(HTTPRequest)
      Debug Str(Nr) + " --> " + StringByteLength(Response$)
   EndIf
EndProcedure

NewList Threads()
For k = 1 To 25
   AddElement(Threads())
   Threads() = CreateThread(@TestThread(), k)
Next
ForEach Threads()
   WaitThread(Threads())
Next

Expected: For every Request/Thread the same amount of bytes received

But the result is:
Code:
0 --> 25684
13 --> 25684
4 --> 25684
2 --> 25684
15 --> 25684
8 --> 25684
24 --> 25684
25 --> 25684
23 --> 25684
12 --> 25684
6 --> 25684
10 --> 25684
19 --> 25684
3 --> 566
17 --> 566
7 --> 566
22 --> 25684
9 --> 566
5 --> 566
18 --> 566
21 --> 566
1 --> 25684
20 --> 566
11 --> 566
14 --> 566
16 --> 566

_________________
As you walk on by, Will you call my name? Or will you walk away?


Top
 Profile  
Reply with quote  
 Post subject: Re: HTTPRequest() not threadsafe?
PostPosted: Tue Mar 17, 2020 9:42 am 
Offline
Addict
Addict

Joined: Sat Feb 08, 2014 3:26 pm
Posts: 966
Add a little delay between requests.
Without that, server have no time to make page and block as flood (reply: 403 Forbidden)
Code:
InitNetwork()

Procedure TestThread(Nr)
   Protected Response$ = ""
   Protected HttpRequest = HTTPRequest(#PB_HTTP_Post, "https://www.purebasic.com/", "" + Str(Nr))
   If HTTPRequest
      Response$ = HTTPInfo(HTTPRequest, #PB_HTTP_Response)
      FinishHTTP(HTTPRequest)
      Debug Str(Nr) + " --> " + StringByteLength(Response$)
      Debug Left(Response$, 100) ; See what happend if no delay
   EndIf
EndProcedure

NewList Threads()
For k = 1 To 25
   AddElement(Threads())
   Threads() = CreateThread(@TestThread(), k)
   Delay(200) ; Add a little delay
Next
ForEach Threads()
   WaitThread(Threads())
Next

:wink:


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: ludoke and 38 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye