Thread: [Py4j-users] Starting/stopping py4j server from within python?
Status: Beta
Brought to you by:
barthe
From: Thomas N. <tom...@gm...> - 2014-12-08 20:47:02
|
Hello, What would be the best way for me to start and stop the py4j server from within python? I know I could use > os.system("java ...") to start the server. I guess I would need a subprocess for that not to block. Then to stop it I could send it a kill signal. Is this the best way to do it? Is there a way for me to do this in a less directly-managed fashion? This is okay for me, but the users of what I'm making probably would have trouble... Cheers, Thomas |
From: Barthelemy D. <bar...@in...> - 2014-12-08 21:09:20
|
Hi, it depends on your use case. Most of the examples start a server in the Java main method so if you do gateway.shutdown() on the Python side, it will (1) send a shutdown signal on the Java side, (2) the Java server will stops, and (3) because there are no more active threads, the JVM will exit. You can even do something harsher from Python: gateway.jvm.System.exit(0) Does that help? Barthelemy On Mon, Dec 8, 2014 at 3:46 PM, Thomas Nyberg <tom...@gm...> wrote: > Hello, > > What would be the best way for me to start and stop the py4j server from > within python? I know I could use > > > os.system("java ...") > > to start the server. I guess I would need a subprocess for that not to > block. Then to stop it I could send it a kill signal. Is this the best > way to do it? Is there a way for me to do this in a less > directly-managed fashion? This is okay for me, but the users of what I'm > making probably would have trouble... > > Cheers, > Thomas > > ------------------------------------------------------------------------------ > Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server > from Actuate! Instantly Supercharge Your Business Reports and Dashboards > with Interactivity, Sharing, Native Excel Exports, App Integration & more > Get technology previously reserved for billion-dollar corporations, FREE > http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk > _______________________________________________ > Py4j-users mailing list > Py4...@li... > https://lists.sourceforge.net/lists/listinfo/py4j-users |
From: Thomas N. <tom...@gm...> - 2014-12-08 21:19:20
|
Great that's a nice way to avoid sending any explicit kill signals. Also that's probably easy enough for my users to manage. What would be really nice is if there could be some sort of garbage collector hook that would send the shutdown signal automatically. That way you could maybe create a class that automatically started the server and then when whatever you're using goes out of scope, your hook could shutdown the server. I'm sure something like this is theoretically possible, but I've never worked with that sort of thing. Then maybe on init one could check if the port is in use (i.e. by another py4j server) and choose a different port in that case. This is all probably overkill though. Your method is great for now. Thanks! On 12/08/2014 04:09 PM, Barthelemy Dagenais wrote: > Hi, > > it depends on your use case. Most of the examples start a server in > the Java main method so if you do gateway.shutdown() on the Python > side, it will (1) send a shutdown signal on the Java side, (2) the > Java server will stops, and (3) because there are no more active > threads, the JVM will exit. > > You can even do something harsher from Python: > > gateway.jvm.System.exit(0) > > Does that help? > > Barthelemy > > On Mon, Dec 8, 2014 at 3:46 PM, Thomas Nyberg <tom...@gm...> wrote: >> Hello, >> >> What would be the best way for me to start and stop the py4j server from >> within python? I know I could use >> >> > os.system("java ...") >> >> to start the server. I guess I would need a subprocess for that not to >> block. Then to stop it I could send it a kill signal. Is this the best >> way to do it? Is there a way for me to do this in a less >> directly-managed fashion? This is okay for me, but the users of what I'm >> making probably would have trouble... >> >> Cheers, >> Thomas >> >> ------------------------------------------------------------------------------ >> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server >> from Actuate! Instantly Supercharge Your Business Reports and Dashboards >> with Interactivity, Sharing, Native Excel Exports, App Integration & more >> Get technology previously reserved for billion-dollar corporations, FREE >> http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk >> _______________________________________________ >> Py4j-users mailing list >> Py4...@li... >> https://lists.sourceforge.net/lists/listinfo/py4j-users > > ------------------------------------------------------------------------------ > Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server > from Actuate! Instantly Supercharge Your Business Reports and Dashboards > with Interactivity, Sharing, Native Excel Exports, App Integration & more > Get technology previously reserved for billion-dollar corporations, FREE > http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk > _______________________________________________ > Py4j-users mailing list > Py4...@li... > https://lists.sourceforge.net/lists/listinfo/py4j-users > |
From: Barthelemy D. <bar...@in...> - 2014-12-09 01:27:33
|
Hi, here are a few pointers that may be useful to you: 1. The py4j.java_gateway.launch_gateway enables you to start a JVM and kill it when the Python process dies. 2. It's possible to launch the Java server with port 0 (ephemeral port): this takes the first accessible port. You then need a way to communicate this port to the Python side (launch_gateway uses a pipe) 3. Py4J supports garbage collection: if a python reference to a Java object is garbage collected, the reference to the Java object is released. This uses the weakref.ref(obj, callback) method. You may want to use something like that to know when to stop the Java server. Please note that finalizers, as it is the case with most programming languages, are not guaranteed to be called, but they are good enough for most use cases. Barthelemy On Mon, Dec 8, 2014 at 4:19 PM, Thomas Nyberg <tom...@gm...> wrote: > Great that's a nice way to avoid sending any explicit kill signals. Also > that's probably easy enough for my users to manage. > > What would be really nice is if there could be some sort of garbage > collector hook that would send the shutdown signal automatically. That > way you could maybe create a class that automatically started the server > and then when whatever you're using goes out of scope, your hook could > shutdown the server. I'm sure something like this is theoretically > possible, but I've never worked with that sort of thing. Then maybe on > init one could check if the port is in use (i.e. by another py4j server) > and choose a different port in that case. > > This is all probably overkill though. Your method is great for now. Thanks! > > On 12/08/2014 04:09 PM, Barthelemy Dagenais wrote: >> Hi, >> >> it depends on your use case. Most of the examples start a server in >> the Java main method so if you do gateway.shutdown() on the Python >> side, it will (1) send a shutdown signal on the Java side, (2) the >> Java server will stops, and (3) because there are no more active >> threads, the JVM will exit. >> >> You can even do something harsher from Python: >> >> gateway.jvm.System.exit(0) >> >> Does that help? >> >> Barthelemy >> >> On Mon, Dec 8, 2014 at 3:46 PM, Thomas Nyberg <tom...@gm...> wrote: >>> Hello, >>> >>> What would be the best way for me to start and stop the py4j server from >>> within python? I know I could use >>> >>> > os.system("java ...") >>> >>> to start the server. I guess I would need a subprocess for that not to >>> block. Then to stop it I could send it a kill signal. Is this the best >>> way to do it? Is there a way for me to do this in a less >>> directly-managed fashion? This is okay for me, but the users of what I'm >>> making probably would have trouble... >>> >>> Cheers, >>> Thomas >>> >>> ------------------------------------------------------------------------------ >>> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server >>> from Actuate! Instantly Supercharge Your Business Reports and Dashboards >>> with Interactivity, Sharing, Native Excel Exports, App Integration & more >>> Get technology previously reserved for billion-dollar corporations, FREE >>> http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk >>> _______________________________________________ >>> Py4j-users mailing list >>> Py4...@li... >>> https://lists.sourceforge.net/lists/listinfo/py4j-users >> >> ------------------------------------------------------------------------------ >> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server >> from Actuate! Instantly Supercharge Your Business Reports and Dashboards >> with Interactivity, Sharing, Native Excel Exports, App Integration & more >> Get technology previously reserved for billion-dollar corporations, FREE >> http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk >> _______________________________________________ >> Py4j-users mailing list >> Py4...@li... >> https://lists.sourceforge.net/lists/listinfo/py4j-users >> > > ------------------------------------------------------------------------------ > Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server > from Actuate! Instantly Supercharge Your Business Reports and Dashboards > with Interactivity, Sharing, Native Excel Exports, App Integration & more > Get technology previously reserved for billion-dollar corporations, FREE > http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk > _______________________________________________ > Py4j-users mailing list > Py4...@li... > https://lists.sourceforge.net/lists/listinfo/py4j-users |