Advertisement
YoungJules

Parallel Gets

Mar 30th, 2011
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GAMBAS 2.92 KB | None | 0 0
  1. ' Gambas module file
  2.  
  3. ' How many parallel gets we'll allow
  4. ' (make sure you define all the relevant xxx00_Finished(), xxx01_Finished()... xxx99_Finished() SUBs)
  5. PRIVATE CONST MAX_SLOTS AS Integer = 10
  6.  
  7. ' An array of booleans indicating 'slots' for http clients working in parallel
  8. ' If it's FALSE it's NOT BUSY, TRUE means this slot is BUSY
  9. PRIVATE slots AS NEW Boolean[MAX_SLOTS]
  10.  
  11. ' An Object array to hold our httpclients
  12. PRIVATE getters AS NEW Object[MAX_SLOTS]
  13.  
  14. ' A test SUB, if you call this you should find slots 0 to 4 get freed up depending on how fast each web site responds to the get (so sometimes it'll be 0,1,2,3,4 or maybe 0,2,1,3,4 or 0,1,2,4,3 etc. etc.)
  15. PUBLIC SUB getURLs()
  16.  
  17.   DIM sURL AS String
  18.   DIM rURLs AS String[] = ["http://www.google.com", "http://www.cnn.com", "http://www.slashdot.com", "http://www.gambas.org", "http://www.youtube.com"]
  19.   FOR EACH sURL IN rURLs
  20.     doGet(sURL)
  21.   NEXT
  22.  
  23.  
  24. END
  25.  
  26. ' Essentially your code out of the FOR loop...
  27.  
  28. PRIVATE SUB doGet(sURL AS String)
  29.  
  30.   DIM freeSlot AS Integer
  31.   freeSlot = firstFreeSlot()
  32.   PRINT "First free slot:" & freeSlot
  33.   ' Mark the slot busy
  34.   slots[freeSlot] = TRUE
  35.   getters[freeSlot] = NEW HttpClient AS "hPachubeFetch0" & freeSlot
  36.   getters[freeSlot].URL = sUrl
  37.   getters[freeSlot].TimeOut = 8
  38.   'hPachubeFetch.Tag = sFeed & "|0|12" ' feed | datastream | device id
  39.   'hPachubeFetch.Auth = 1
  40.   'hPachubeFetch.User = "user"
  41.   'hPachubeFetch.Password = "password"
  42.   getters[freeSlot].Async = TRUE
  43.   getters[freeSlot].Get()  
  44.  
  45.  
  46. END
  47.  
  48. ' Just determine the index of the first free (FALSE) slot
  49. PRIVATE FUNCTION firstFreeSlot() AS Integer
  50.  
  51.   DIM idx AS Integer
  52.   DIM slot AS Boolean
  53.   FOR idx = 0 TO slots.Count - 1
  54.     IF slots[idx] = FALSE THEN
  55.       RETURN idx
  56.     END IF
  57.   NEXT
  58.   RETURN -1
  59. END
  60.  
  61. ' Is there a better way to do this??
  62. ' Simple enough but quite some copy-paste and you need to correct
  63. ' stuff 10 times... but it works!
  64. PUBLIC SUB hPachubeFetch00_Finished()
  65.  
  66.   slots[0] = FALSE
  67.   PRINT "Slot 0 freed up"
  68.  
  69. END
  70.  
  71. PUBLIC SUB hPachubeFetch01_Finished()
  72.  
  73.   slots[1] = FALSE
  74.   PRINT "Slot 1 freed up"
  75.  
  76. END
  77.  
  78. PUBLIC SUB hPachubeFetch02_Finished()
  79.  
  80.   slots[2] = FALSE
  81.   PRINT "Slot 2 freed up"  
  82.  
  83. END
  84.  
  85. PUBLIC SUB hPachubeFetch03_Finished()
  86.  
  87.   slots[3] = FALSE
  88.   PRINT "Slot 3 freed up"
  89.  
  90. END
  91. PUBLIC SUB hPachubeFetch04_Finished()
  92.  
  93.   slots[4] = FALSE
  94.   PRINT "Slot 4 freed up"
  95.  
  96. END
  97.  
  98. PUBLIC SUB hPachubeFetch05_Finished()
  99.  
  100.   slots[5] = FALSE
  101.   PRINT "Slot 5 freed up"  
  102.  
  103. END
  104. PUBLIC SUB hPachubeFetch06_Finished()
  105.  
  106.   slots[6] = FALSE
  107.   PRINT "Slot 6 freed up"  
  108.  
  109. END
  110.  
  111. PUBLIC SUB hPachubeFetch07_Finished()
  112.  
  113.   slots[7] = FALSE
  114.   PRINT "Slot 7 freed up"  
  115.  
  116. END
  117. PUBLIC SUB hPachubeFetch08_Finished()
  118.  
  119.   slots[8] = FALSE
  120.   PRINT "Slot 8 freed up"  
  121.  
  122. END
  123.  
  124. PUBLIC SUB hPachubeFetch09_Finished()
  125.  
  126.   slots[9] = FALSE
  127.   PRINT "Slot 9 freed up"  
  128.  
  129. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement