pub struct CodeChannel { /* private fields */ }Expand description
A cloneable, blocking result channel. Multiple consumers may call recv;
disconnected means shut_down_resrap has finished draining every result.
Implementations§
Source§impl CodeChannel
impl CodeChannel
Sourcepub fn recv(&self) -> Result<CodeGenRes, RecvError>
pub fn recv(&self) -> Result<CodeGenRes, RecvError>
Examples found in repository?
examples/multithreaded.rs (line 20)
9fn main() -> Result<(), String> {
10 let mut resrap = ResrapMT::new(4, 256);
11 templates::load(&mut resrap, templates::Lang::Python)?;
12 resrap.start_resrap();
13
14 let channel = resrap.get_code_channel();
15 for id in 0..8u64 {
16 resrap.generate_with_seeded(&format!("job-{id}"), "python", "program", id + 1, 80)?;
17 }
18
19 for _ in 0..8 {
20 let result = channel.recv().map_err(|err| err.to_string())?;
21 println!("--- {} ---\n{}\n", result.id, result.code);
22 }
23
24 resrap.shutdown_resrap();
25 Ok(())
26}More examples
examples/memory.rs (line 82)
39fn main() {
40 report("baseline");
41
42 let mut resrap = Resrap::new();
43 for lang in templates::Lang::ALL {
44 templates::load(&mut resrap, lang).unwrap();
45 }
46 report("all 10 templates loaded");
47
48 let outputs: Vec<String> = (0..10_000)
49 .map(|i| resrap.generate_with_seeded("c", "program", i % 97 + 1, 200))
50 .collect();
51 let output_bytes: usize = outputs.iter().map(String::len).sum();
52 report("10,000 outputs retained (C, 200 tokens)");
53 println!(
54 " retained output: {:.1} MiB, average {:.0} B/output",
55 output_bytes as f64 / (1024.0 * 1024.0),
56 output_bytes as f64 / outputs.len() as f64
57 );
58
59 drop(outputs);
60 report("outputs dropped");
61
62 let mut mt = ResrapMT::new(8, 4096);
63 templates::load(&mut mt, "c").unwrap();
64 mt.start_resrap();
65 let channel = mt.get_code_channel();
66
67 let jobs = 10_000;
68 for i in 0..jobs {
69 mt.generate_with_seeded(
70 &format!("job-{i}"),
71 "c",
72 "program",
73 (i as u64) % 97 + 1,
74 200,
75 )
76 .unwrap();
77 }
78 report("MT results buffered, consumer idle");
79
80 let mut received = 0usize;
81 for _ in 0..jobs {
82 received += channel.recv().unwrap().code.len();
83 }
84 report("MT results drained");
85 println!(
86 " received output: {:.1} MiB",
87 received as f64 / (1024.0 * 1024.0)
88 );
89
90 drop(channel);
91 mt.shutdown_resrap();
92 drop(mt);
93 report("MT instance dropped");
94 // Run the long-output case last: large allocations can change glibc's
95 // mmap threshold and make later RSS figures depend on allocator history.
96 let long = resrap.generate_with_seeded("c", "program", 7, 1_000_000);
97 report("one 1M-token output retained");
98 println!(
99 " retained output: {:.1} MiB",
100 long.len() as f64 / (1024.0 * 1024.0)
101 );
102 drop(long);
103 report("1M-token output dropped");
104}pub fn try_recv(&self) -> Result<CodeGenRes, TryRecvError>
pub fn recv_timeout( &self, timeout: Duration, ) -> Result<CodeGenRes, RecvTimeoutError>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CodeChannel
impl RefUnwindSafe for CodeChannel
impl Send for CodeChannel
impl Sync for CodeChannel
impl Unpin for CodeChannel
impl UnsafeUnpin for CodeChannel
impl UnwindSafe for CodeChannel
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more