@@ -194,29 +194,24 @@ impl PythonActorMesh {
194
194
. map ( ActorRef :: into_actor_id)
195
195
. map ( PyActorId :: from) )
196
196
}
197
-
198
- fn supervise ( & self , py : Python < ' _ > , receiver : Bound < ' _ , PyAny > ) -> PyResult < PyObject > {
199
- if let Ok ( r) = receiver. extract :: < PyRef < PythonPortReceiver > > ( ) {
200
- let rx = SupervisedPythonPortReceiver {
201
- inner : r. inner ( ) ,
202
- monitor : ActorMeshMonitor {
203
- receiver : SharedCell :: from ( Mutex :: new ( self . user_monitor_sender . subscribe ( ) ) ) ,
204
- } ,
205
- } ;
206
- rx. into_py_any ( py)
207
- } else if let Ok ( r) = receiver. extract :: < PyRef < PythonOncePortReceiver > > ( ) {
208
- let rx = SupervisedPythonOncePortReceiver {
209
- inner : r. inner ( ) ,
210
- monitor : ActorMeshMonitor {
211
- receiver : SharedCell :: from ( Mutex :: new ( self . user_monitor_sender . subscribe ( ) ) ) ,
197
+ fn supervision_event ( & self ) -> PyResult < PyPythonTask > {
198
+ let mut receiver = self . user_monitor_sender . subscribe ( ) ;
199
+ PyPythonTask :: new ( async move {
200
+ let event = receiver. recv ( ) . await ;
201
+ let event = match event {
202
+ Ok ( Some ( event) ) => PyActorSupervisionEvent :: from ( event. clone ( ) ) ,
203
+ Ok ( None ) | Err ( _) => PyActorSupervisionEvent {
204
+ // Dummy actor as placeholder to indicate the whole mesh is stopped
205
+ // TODO(albertli): remove this when pushing all supervision logic to rust.
206
+ actor_id : id ! ( default [ 0 ] . actor[ 0 ] ) . into ( ) ,
207
+ actor_status : "actor mesh is stopped due to proc mesh shutdown" . to_string ( ) ,
212
208
} ,
213
209
} ;
214
- rx. into_py_any ( py)
215
- } else {
216
- Err ( PyTypeError :: new_err (
217
- "Expected a PortReceiver or OncePortReceiver" ,
218
- ) )
219
- }
210
+ Ok ( PyErr :: new :: < SupervisionError , _ > ( format ! (
211
+ "supervision error: {:?}" ,
212
+ event
213
+ ) ) )
214
+ } )
220
215
}
221
216
222
217
#[ pyo3( signature = ( * * kwargs) ) ]
@@ -406,106 +401,6 @@ impl Drop for PythonActorMesh {
406
401
}
407
402
}
408
403
409
- #[ derive( Debug , Clone ) ]
410
- struct ActorMeshMonitor {
411
- receiver : SharedCell < Mutex < tokio:: sync:: broadcast:: Receiver < Option < ActorSupervisionEvent > > > > ,
412
- }
413
-
414
- impl ActorMeshMonitor {
415
- pub async fn next ( & self ) -> Result < PyActorSupervisionEvent , PyErr > {
416
- let receiver = self . receiver . clone ( ) ;
417
- let receiver = receiver
418
- . borrow ( )
419
- . expect ( "`Actor mesh receiver` is shutdown" ) ;
420
- let mut receiver = receiver. lock ( ) . await ;
421
- let event = receiver. recv ( ) . await ;
422
- Ok ( match event {
423
- Ok ( Some ( event) ) => PyActorSupervisionEvent :: from ( event. clone ( ) ) ,
424
- Ok ( None ) | Err ( _) => PyActorSupervisionEvent {
425
- // Dummy actor as placeholder to indicate the whole mesh is stopped
426
- // TODO(albertli): remove this when pushing all supervision logic to rust.
427
- actor_id : id ! ( default [ 0 ] . actor[ 0 ] ) . into ( ) ,
428
- actor_status : "actor mesh is stopped due to proc mesh shutdown" . to_string ( ) ,
429
- } ,
430
- } )
431
- }
432
- }
433
-
434
- // Values of this type can only be created by calling
435
- // `PythonActorMesh::supervise()`.
436
- #[ pyclass(
437
- name = "SupervisedPortReceiver" ,
438
- module = "monarch._rust_bindings.monarch_hyperactor.actor_mesh"
439
- ) ]
440
- struct SupervisedPythonPortReceiver {
441
- inner : Arc < tokio:: sync:: Mutex < PortReceiver < PythonMessage > > > ,
442
- monitor : ActorMeshMonitor ,
443
- }
444
-
445
- #[ pymethods]
446
- impl SupervisedPythonPortReceiver {
447
- fn __repr__ ( & self ) -> & ' static str {
448
- "<SupervisedPortReceiver>"
449
- }
450
-
451
- fn recv_task ( & mut self ) -> PyPythonTask {
452
- let receiver = self . inner . clone ( ) ;
453
- let monitor = self . monitor . clone ( ) ;
454
- PythonTask :: new ( async move {
455
- let mut receiver = receiver. lock ( ) . await ;
456
- let result = tokio:: select! {
457
- result = receiver. recv( ) => {
458
- result. map_err( |err| PyErr :: new:: <PyEOFError , _>( format!( "port closed: {}" , err) ) )
459
- }
460
- event = monitor. next( ) => {
461
- Python :: with_gil( |_py| {
462
- Err ( PyErr :: new:: <SupervisionError , _>( format!( "supervision error: {:?}" , event) ) )
463
- } )
464
- }
465
- } ;
466
- result. and_then ( |message : PythonMessage | Python :: with_gil ( |py| message. into_py_any ( py) ) )
467
- } ) . into ( )
468
- }
469
- }
470
-
471
- // Values of this type can only be created by calling
472
- // `PythonActorMesh::supervise()`.
473
- #[ pyclass(
474
- name = "SupervisedOncePortReceiver" ,
475
- module = "monarch._rust_bindings.monarch_hyperactor.actor_mesh"
476
- ) ]
477
- struct SupervisedPythonOncePortReceiver {
478
- inner : Arc < std:: sync:: Mutex < Option < OncePortReceiver < PythonMessage > > > > ,
479
- monitor : ActorMeshMonitor ,
480
- }
481
-
482
- #[ pymethods]
483
- impl SupervisedPythonOncePortReceiver {
484
- fn __repr__ ( & self ) -> & ' static str {
485
- "<SupervisedOncePortReceiver>"
486
- }
487
-
488
- fn recv_task ( & mut self ) -> PyResult < PyPythonTask > {
489
- let Some ( receiver) = self . inner . lock ( ) . unwrap ( ) . take ( ) else {
490
- return Err ( PyErr :: new :: < PyValueError , _ > ( "OncePort is already used" ) ) ;
491
- } ;
492
- let monitor = self . monitor . clone ( ) ;
493
- Ok ( PythonTask :: new ( async move {
494
- let result = tokio:: select! {
495
- result = receiver. recv( ) => {
496
- result. map_err( |err| PyErr :: new:: <PyEOFError , _>( format!( "port closed: {}" , err) ) )
497
- }
498
- event = monitor. next( ) => {
499
- Python :: with_gil( |_py| {
500
- Err ( PyErr :: new:: <SupervisionError , _>( format!( "supervision error: {:?}" , event) ) )
501
- } )
502
- }
503
- } ;
504
- result. and_then ( |message : PythonMessage | Python :: with_gil ( |py| message. into_py_any ( py) ) )
505
- } ) . into ( ) )
506
- }
507
- }
508
-
509
404
#[ pyclass(
510
405
name = "ActorSupervisionEvent" ,
511
406
module = "monarch._rust_bindings.monarch_hyperactor.actor_mesh"
@@ -543,8 +438,6 @@ impl From<ActorSupervisionEvent> for PyActorSupervisionEvent {
543
438
pub fn register_python_bindings ( hyperactor_mod : & Bound < ' _ , PyModule > ) -> PyResult < ( ) > {
544
439
hyperactor_mod. add_class :: < PythonActorMesh > ( ) ?;
545
440
hyperactor_mod. add_class :: < PythonActorMeshRef > ( ) ?;
546
- hyperactor_mod. add_class :: < SupervisedPythonPortReceiver > ( ) ?;
547
- hyperactor_mod. add_class :: < SupervisedPythonOncePortReceiver > ( ) ?;
548
441
hyperactor_mod. add_class :: < PyActorSupervisionEvent > ( ) ?;
549
442
Ok ( ( ) )
550
443
}
0 commit comments