File: tsld021.htm

package info (click to toggle)
aolserver4 4.5.1-15.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,772 kB
  • sloc: ansic: 45,120; tcl: 5,532; sh: 1,021; makefile: 380; pascal: 219; php: 13
file content (120 lines) | stat: -rw-r--r-- 2,034 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

<HTML>

<HEAD>

<meta name="GENERATOR" content="Microsoft Internet Assistant for PowerPoint 97">
 <TITLE>Example: Event-Driven vs Thread I/O</TITLE> 
</HEAD>

<BODY     >

 <H1>Example: Event-Driven vs Thread I/O</H1> 
 <P><UL>
<LI><H2>I/O bound apps are sometimes easier with threads:</H2>
</UL></P>
<P><UL>
global done count
</UL><UL>

</UL><UL>
proc my.read {sock} {
</UL><UL>
  global done count
</UL><UL>
  read $sock
</UL><UL>
  if [eof $sock] {
</UL><UL>
    close $sock
</UL><UL>
    if {[incr count -1] == 0} {
</UL><UL>
      set done 1
</UL><UL>
    }
</UL><UL>
  }
</UL><UL>
}
</UL><UL>

</UL><UL>
proc my.write {sock} {
</UL><UL>
  puts $sock "GET /index.html\r\n"
</UL><UL>
  flush $sock
</UL><UL>
  fileevent $sock readable "my.read $sock"
</UL><UL>
}
</UL><UL>

</UL><UL>
set count 0
</UL><UL>
foreach host { host1 host2 host3 host4} {
</UL><UL>
  incr count
</UL><UL>
  set sock [socket -async $host 80]
</UL><UL>
  fconfigure $sock -blocking off
</UL><UL>
  fileevent $sock writable "my.write $sock"
</UL><UL>
}
</UL><UL>

</UL><UL>
vwait done</UL></P>
<P><UL>
proc my.thread {host} {
</UL><UL>
  set sock [socket $host 80]
</UL><UL>
  puts $sock "GET /index.html\r\n"
</UL><UL>
  flush $sock
</UL><UL>
  read $sock
</UL><UL>
  close $sock
</UL><UL>
}
</UL><UL>

</UL><UL>
foreach host {host1 host2 host3 host4} {
</UL><UL>
  lappend tids [ns_thread begin my.thread $host"]
</UL><UL>
}
</UL><UL>

</UL><UL>
foreach tid $tids {
</UL><UL>
  ns_thread wait $tid
</UL><UL>
}</UL></P>
<P><UL>
Events</UL></P>
<P><UL>
Threads</UL></P>
<P></P> 
<P>
<TABLE>
  <TD HEIGHT=100 WIDTH=100> <A HREF="tsld020.htm">Previous slide</A> </TD>
  <TD HEIGHT=100 WIDTH=100> <A HREF="tsld022.htm">Next slide</A> </TD>
  <TD HEIGHT=100 WIDTH=150> <A HREF="tsld001.htm">Back to first slide</A> </TD>
  <TD HEIGHT=100 WIDTH=150> <A HREF="sld021.htm">View graphic version</A> </TD>
</TABLE>
<BR>
</P>



</Body>
</HTML>