48 mzlib/thread

▼ |  | Mz Lib: Legacy Libraries

1 | mzlib/ a-signature
2 | mzlib/ a-unit
3 | mzlib/ async-channel
4 | mzlib/ awk
5 | mzlib/ class
6 | mzlib/ cm
7 | mzlib/ cm-accomplice
8 | mzlib/ cmdline
9 | mzlib/ cml
10 | mzlib/ compat
11 | mzlib/ compile
12 | mzlib/ contract
13 | mzlib/ control
14 | mzlib/ date
15 | mzlib/ deflate
16 | mzlib/ defmacro
17 | mzlib/ etc
18 | mzlib/ file
19 | mzlib/ for
20 | mzlib/ foreign
21 | mzlib/ include
22 | mzlib/ inflate
23 | mzlib/ integer-set
24 | mzlib/ kw
25 | mzlib/ list
26 | mzlib/ match
27 | mzlib/ math
28 | mzlib/ md5
29 | mzlib/ os
30 | mzlib/ pconvert
31 | mzlib/ pconvert-prop
32 | mzlib/ plt-match
33 | mzlib/ port
34 | mzlib/ pregexp
35 | mzlib/ pretty
36 | mzlib/ process
37 | mzlib/ restart
38 | mzlib/ runtime-path
39 | mzlib/ sandbox
40 | mzlib/ sendevent
41 | mzlib/ serialize
42 | mzlib/ shared
43 | mzlib/ string
44 | mzlib/ struct
45 | mzlib/ stxparam
46 | mzlib/ surrogate
47 | mzlib/ tar
48 | mzlib/ thread
49 | mzlib/ trace
50 | mzlib/ traceld
51 | mzlib/ trait
52 | mzlib/ transcr
53 | mzlib/ unit
54 | mzlib/ unit-exptime
55 | mzlib/ unit200
56 | mzlib/ unitsig200
57 | mzlib/ zip
| Bibliography
| Index

On this page:

consumer-thread
run-server

Racket
top contents ← prev up next →

(require mzlib/thread) | package: compatibility-lib

> NOTE: This library is deprecated; use racket/engine, instead.

Re-exports the bindings from racket/engine under different names and also provides two extra bindings. The renamings are:

- engine as coroutine

- engine? as coroutine?

- engine-run as coroutine-run

- engine-result as coroutine-result

- engine-kill as coroutine-kill

> > procedure
> > (consumer-thread f [init]) |  | → |  |
> > thread? |  | procedure?
> f : procedure?
> init : (-> any) = void

Returns two values: a thread descriptor for a new thread, and a procedure with the same arity as f.

When the returned procedure is applied, its arguments are queued to be passed on to f, and #<void> is immediately returned. The thread created by consumer-thread dequeues arguments and applies f to them, removing a new set of arguments from the queue only when the previous application of f has completed; if f escapes from a normal return (via an exception or a continuation), the f-applying thread terminates.

The init argument is a procedure of no arguments; if it is provided, init is called in the new thread immediately after the thread is created.

> > procedure
> > (run-server |  | port-no |  |  |  |
> > |  | conn-proc |  |  |  |
> > |  | conn-timeout |  |  |  |
> > | [ | handler |  |  |  |
> > |  | listen |  |  |  |
> > |  | close |  |  |  |
> > |  | accept |  |  |  |
> > |  | accept/break]) |  | → |  | void?
> port-no : (integer-in 1 65535)
> conn-proc : (input-port? output-port? . -> . any)
> conn-timeout : (and/c real? (not/c negative?))
> handler : (exn? . -> . any/c) = void
> | listen |  | : |  |
> ((integer-in 1 65535) (one-of/c 5) (one-of/c #t)
> . -> . listener?)
> |  |  | = |  | tcp-listen
> close : (listener? . -> . any) = tcp-close
> | accept |  | : |  | (listener? . ->* . (input-port? output-port?))
> |  |  | = |  | tcp-accept
> | accept/break |  | : |  | (listener? . ->* . (input-port? output-port?))
> |  |  | = |  | tcp-accept/enable-break

Executes a TCP server on the port indicated by port-no. When a connection is made by a client, conn is called with two values: an input port to receive from the client, and an output port to send to the client.

Each client connection is managed by a new custodian, and each call to conn occurs in a new thread (managed by the connection’s custodian). If the thread executing conn terminates for any reason (e.g., conn returns), the connection’s custodian is shut down. Consequently, conn need not close the ports provided to it. Breaks are enabled in the connection thread if breaks are enabled when run-server is called.

To facilitate capturing a continuation in one connection thread and invoking it in another, the parameterization of the run-server call is used for every call to handler. In this parameterization and for the connection’s thread, the current-custodian parameter is assigned to the connection’s custodian.

If conn-timeout is not #f, then it must be a non-negative number specifying the time in seconds that a connection thread is allowed to run before it is sent a break signal. Then, if the thread runs longer than (* conn-timeout 2) seconds, then the connection’s custodian is shut down. If conn-timeout is #f, a connection thread can run indefinitely.

If handler is provided, it is passed exceptions related to connections (i.e., exceptions not caught by conn-proc, or exceptions that occur when trying to accept a connection). The default handler ignores the exception and returns #<void>.

The run-server function uses listen, close, accept and accept/break in the same way as it might use tcp-listen, tcp-close, tcp-accept, and tcp-accept/enable-break to accept connections. Provide alternate procedures to use an alternate communication protocol (such as SSL) or to supply optional arguments in the use of tcp-listen. The listener? part of the contract indicates that the procedures must all work on the same kind of listener value.

The run-server procedure loops to serve client connections, so it never returns. If a break occurs, the loop will cleanly shut down the server, but it will not terminate active connections.

top contents ← prev up next →