Spycams and spawn...

Guiserver, GTK-server, OpenGL, PostScript,
HTML 5, MIDI, IDE
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Spycams and spawn...

Post by cormullion »

I've been tinkering with this project today. It's a bit odd (dubious even!), but my main question is "Is this a good use for spawn/sync?". I've rarely used it, but it occurred to me that it offered a useful 'timeout' feature that made the programming easier. But is it suitable for use in this way?

Also, I don't know how to handle errors generated by spawn/sync. Help would be gratefully received!

Here's the code:

Code: Select all

#!/usr/bin/env newlisp

(load (append (env "NEWLISPDIR") "/guiserver.lsp"))
(gs:init) 
(gs:frame 'Surveillance 100 100 650 490 "Surveillance")
(gs:set-border-layout 'Surveillance)
(gs:canvas 'MyCanvas)

(gs:set-background 'MyCanvas gs:gray)
(gs:add-to 'Surveillance 'MyCanvas "center")
(gs:set-visible 'Surveillance true)

(seed (date-value))

(set 'web-cams '(
("http://129.78.249.82/jpg/image.jpg" "Australia" "Sydney" -33.8833007812 151.216705322) 
("http://137.229.242.21/jpg/image.jpg" "United States" "Fairbanks" 64.8171005249 -147.87840271) 
("http://62.168.0.121/jpg/image.jpg" "Czech Republic" "Teplice" 50.6333007812 13.8332996368) 
("http://213.192.14.189/jpg/image.jpg" "Czech Republic" "Prerov" 49.4500007629 17.4500007629) 
("http://62.177.76.144:30000/jpg/image.jpg" "Czech Republic" "Line" 49.7000007629 13.266699791) 
("http://195.84.97.99/jpg/image.jpg" "Sweden" "Ostersund" 63.1833000183 14.6499996185) 
("http://213.189.138.114/jpg/image.jpg" "Switzerland" "Bubendorf" 47.4500007629 7.73320007324) 
("http://81.8.151.136:88/jpg/image.jpg" "Sweden" "Storuman" 65.0999984741 17.1000003815) 
("http://194.132.44.103/jpg/image.jpg" "Sweden" "Hoganas" 56.2000007629 12.5500001907) 
("http://cam.buchs-engineering.com/jpg/image.jpg" "Switzerland" "Zurich" 47.3666992188 8.55000019073) 
("http://87.245.83.189/jpg/image.jpg" "Switzerland" "Schaffhausen" 47.7000007629 8.63329982758) 
("http://86.88.98.78/jpg/image.jpg" "Netherlands" "Geldermalsen" 51.8833007812 5.30000019073) 
("http://131.155.18.5/jpg/image.jpg" "Netherlands" "Eindhoven" 51.4500007629 5.46670007706) 
("http://80.65.200.218/jpg/image.jpg" "Sweden" "Ljusdal" 61.8333015442 16.1000003815) 
("http://130.236.85.241/jpg/image.jpg" "Sweden" "Linkoping" 58.4166984558 15.6167001724) 
("http://64.21.226.243/jpg/image.jpg" "United States" "Devils Lake" 48.1436004639 -98.887298584) 
("http://138.237.46.59/jpg/image.jpg" "United States" "Fort Worth" 32.7714004517 -97.2914962769) 
("http://209.137.243.137/jpg/image.jpg" "United States" "Deer Lodge" 46.379699707 -112.720199585) 
("http://207.111.165.30/jpg/image.jpg" "United States" "Hazel Green" 34.9341011047 -86.5802993774) 
("http://84.32.91.253/jpg/image.jpg" "Lithuania" "Alytus" 54.4000015259 24.0499992371)
("http://80.34.87.7/jpg/image.jpg" "Spain" "Sabadell" 41.5499992371 2.09999990463) 
("http://77.72.148.53/jpg/image.jpg" "Germany" "Flensburg" 54.7832984924 9.43330001831) 
("http://70.169.231.214/jpg/image.jpg" "United States" "Wichita" 38.0 -97.0) 
("http://213.72.190.210/jpg/image.jpg" "United Kingdom" "Coventry" 52.4166984558 -1.54999995232) 
("http://76.208.246.26/jpg/image.jpg" "United States" "Wichita" 38.0 -97.0) 
("http://141.211.26.145/jpg/image.jpg" "United States" "Ann Arbor" 42.292301178 -83.7145004272) 
("http://74.94.178.163/jpg/image.jpg" "United States" "Wichita" 38.0 -97.0) 
("http://www.wbctlalivecam.com/jpg/image.jpg" "United States" "Annandale" 38.8381004333 -77.2121963501) 
("http://213.211.42.237/jpg/image.jpg" "Czech Republic" "Pribyslav" 49.5833015442 15.7332000732) 
("http://131.156.107.87/jpg/image.jpg" "United States" "Dekalb" 41.8963012695 -88.7443008423) 
("http://209.20.250.5/jpg/image.jpg" "United States" "Lynnwood" 47.8305015564 -122.283096313) 
("http://62.176.118.20/jpg/image.jpg" "Bulgaria" "Sofia" 42.6833000183 23.3166999817) 
("http://148.61.253.219/jpg/image.jpg" "United States" "Allendale" 42.9742012024 -85.931098938) 
("http://216.8.159.21/jpg/image.jpg" "Canada" "Tilbury" 42.25 -82.4332962036) 
("http://148.61.139.229/jpg/image.jpg" "United States" "Allendale" 42.9742012024 -85.931098938) 
("http://148.61.171.201/jpg/image.jpg" "United States" "Allendale" 42.9742012024 -85.931098938) 
("http://148.61.37.229/jpg/image.jpg" "United States" "Allendale" 42.9742012024 -85.931098938) 
("http://169.237.137.95/jpg/image.jpg" "United States" "Davis" 38.4828987122 -121.639801025) 
("http://www.carilloncams.com/jpg/image.jpg" "United States" "Lynnwood" 47.8305015564 -122.283096313) 
("http://212.200.143.179/jpg/image.jpg" "Yugoslavia" "Uzice" 43.8558006287 19.8411006927) 
("http://148.61.229.201/jpg/image.jpg" "United States" "Allendale" 42.9742012024 -85.931098938) 
("http://72.236.138.36/jpg/image.jpg" "United States" "Fort Myers" 26.5736999512 -81.8268966675) 
("http://169.237.92.110/jpg/image.jpg" "United States" "Davis" 38.4828987122 -121.639801025) 
("http://www.doylevisualmedia.com/jpg/image.jpg" "United States" "Doylestown" 40.3320999146 -75.1178970337) 
("http://148.61.142.227/jpg/image.jpg" "United States" "Allendale" 42.9742012024 -85.931098938) 
("http://24.111.173.43/jpg/image.jpg" "United States" "Pierre" 44.5421981812 -100.275398254) 
("http://148.61.151.202/jpg/image.jpg" "United States" "Allendale" 42.9742012024 -85.931098938) 
("http://217.14.230.30/jpg/image.jpg" "Austria" "Graz" 47.0666999817 15.4499998093) 
("http://193.213.22.142/jpg/image.jpg" "Norway" "Fornebu" 59.9021987915 10.6291999817) 
("http://145.89.196.199/jpg/image.jpg" "Netherlands" "Utrecht" 52.0833015442 5.13329982758) 
("http://82.198.200.23/jpg/image.jpg" "Germany" "Bremen" 53.0833015442 8.80000019073) 
("http://68.115.237.20/jpg/image.jpg" "United States" "Greenville" 34.8476982117 -82.3790969849) 
("http://128.214.194.71/jpg/image.jpg" "Finland" "Helsinki" 60.1755981445 24.9342002869) 
("http://148.61.97.229/jpg/image.jpg" "United States" "Allendale" 42.9742012024 -85.931098938) 
("http://85.152.15.9/jpg/image.jpg" "Spain" "Oviedo" 43.3666992188 -5.83330011368) 
("http://131.118.39.213/jpg/image.jpg" "United States" "Hyattsville" 39.0007019043 -76.9729995728) 
("http://136.183.7.139/jpg/image.jpg" "United States" "Buffalo" 42.9197006226 -78.8778991699) 
("http://213.211.51.90/jpg/image.jpg" "Czech Republic" "Slapanov" 49.6333007812 14.7833003998) 
("http://66.163.131.195/jpg/image.jpg" "United States" "Leeds" 48.3046989441 -99.3993988037) 
("http://193.213.43.148/jpg/image.jpg" "Norway" "Fornebu" 59.9021987915 10.6291999817) 
("http://64.53.55.34/jpg/image.jpg" "United States" "Viola" 41.1968002319 -90.5848999023) 
("http://82.116.33.70/jpg/image.jpg" "Russian Federation" "Saratov" 51.5666999817 46.0332984924) 
("http://85.97.131.6/jpg/image.jpg" "Turkey" "Istanbul" 41.0186004639 28.9647006989) 
("http://213.3.28.103:8081/jpg/image.jpg" "Switzerland" "Zurich" 47.3666992188 8.55000019073) 
("http://212.42.54.137:8008/jpg/image.jpg" "Russian Federation" "Moscow" 55.7522010803 37.6156005859) 
("http://85.221.118.225/jpg/image.jpg" "Norway" "Hagen" 62.9500007629 10.3332996368) 
("http://62.63.51.15/jpg/image.jpg" "Norway" "Hammerfest" 70.6616973877 23.6882991791) 
("http://157.157.90.210/jpg/image.jpg" "Iceland" "Kupavogur" 64.0999984741 -21.9167003632) 
("http://139.86.48.94/jpg/image.jpg" "Australia" "Mintabie" -27.0 133.0) 
("http://193.252.7.129/jpg/image.jpg" "France" "Paris" 48.8666992188 2.33330011368) 
("http://195.22.74.67/jpg/image.jpg" "Sweden" "Karlskoga" 59.3333015442 14.516699791) 
("http://134.76.249.252/jpg/image.jpg" "Germany" "Gottingen" 51.5332984924 9.93330001831) 
("http://147.134.40.120/jpg/image.jpg" "United States" "Omaha" 41.2916984558 -96.1710968018) 
("http://128.230.208.134/jpg/image.jpg" "United States" "Syracuse" 43.0377006531 -76.1396026611) 
("http://206.140.121.226/jpg/image.jpg" "United States" "Pinckney" 42.4580993652 -83.9468002319) 
("http://205.169.69.56/jpg/image.jpg" "United States" "Greeley" 40.3996009827 -104.800201416) 
("http://148.61.63.218/jpg/image.jpg" "United States" "Allendale" 42.9742012024 -85.931098938) 
("http://147.134.38.233/jpg/image.jpg" "United States" "Omaha" 41.2916984558 -96.1710968018) 
("http://www.ymllalivecam.com/jpg/image.jpg" "United States" "Annandale" 38.8381004333 -77.2121963501) 
("http://156.17.62.173/jpg/image.jpg" "Poland" "Wroclaw" 51.0999984741 17.0333003998) 
("http://87.54.59.228/jpg/image.jpg" "Denmark" "None" 56.0 10.0) 
("http://217.37.180.233:82/jpg/image.jpg" "United Kingdom" "Egham" 51.4166984558 -0.566699981689) 
("http://62.254.171.181/jpg/image.jpg" "United Kingdom" "Reddish" 53.4333000183 -2.15000009537) 
("http://85.19.196.42:8080/jpg/image.jpg" "Norway" "Vennesla" 58.2832984924 7.98320007324) 
("http://24.205.108.46/jpg/image.jpg" "United States" "Sparks" 39.5727005005 -119.617103577) 
("http://147.134.34.236/jpg/image.jpg" "United States" "Omaha" 41.2916984558 -96.1710968018) 
("http://147.134.168.238/jpg/image.jpg" "United States" "Omaha" 41.2916984558 -96.1710968018) 
("http://85.221.85.5:90/jpg/image.jpg" "Norway" "Kyrkseterora" 63.2832984924 9.10000038147) 
("http://217.37.180.233:81/jpg/image.jpg" "United Kingdom" "Egham" 51.4166984558 -0.566699981689)))

(while (gs:check-event 10000) 
  (gs:set-text 'Surveillance "looking...")
  (set 'web-cam (web-cams (rand (length web-cams))))
  (set 'filename (string "/tmp/image" (uuid) ".jpg"))
  (spawn 'p1 (write-file filename (read-file (web-cam 0))))
  (sync 10000)
  (gs:delete-tag 'I)
  (gs:draw-image 'I filename 0 0 640 480)
  (gs:set-text 'Surveillance (format {%s | %s | Latitude %.2f | Longitude %.2f} (rest web-cam)))
  (gs:update)
  (sleep 30000) ; 30 seconds
  )

;eof

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

The way to use 'spawn' is to do start several of them first and then do the 'sync'. The 'sync' will return when all spawned processes have finished. If a spawned process finishes with error then the spawn variable will contain the error information after 'sync' has been done. The error information will always start with the string "ERR:"

In your case you would do something like this:

Code: Select all

(spawn 'p1 (do-webcam-thing 1))
(spawn 'p2 (do-webcam-thing 2))
(spawn 'p3 (do-webcam-thing 3))
...
(sync 60000) ; wait 60 seconds max until all have finished

; now all the p1, p2, p3 ... contain the results or error message
For reading web stuff, spawn is well suited because most of the time the processor spends time waiting for I/O. So it can do stuff in parallel.

You should be able to start a 100 or more of them on Mac OS X. You have to experiment. The more processors you have, the more it will speed up.

Using the inlet parameter in 'sync' you can serve returning childs individually.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks, Lutz. The thing is that I need to do only one at a time (and webcam images are better when they're as recent as possible), but I didn't quite see how to do the "(write-file filename (read-file .." work asynchronously, with a timeout.

I suppose I could write a loop and a timer? But spawn/sync seemed easier, somehow.

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Wow ....what are you spying on ;-)
-- (define? (Cornflakes))

Locked