Bokeh Server Executed my functions twice on import?

Hi team,

I have a simple bokeh data visualization. The code is organized in the following way:

···

================================================================

  • data.py
  • process_map_data

  • process_ts_data

  • main.py

================================================================

process_map_data & process_ts_data are simply helper functions for querying.

def process_map_data(end_date):

  • …*

  • print "Finished processing [map data] … "*

def process_ts_data(end_date):

  • …*

  • print "Finished processing [ts data] … "*

In the main.py file, I imported the two functions because they are convenient to use, very much in the same style as the Gapminder example on the official website.

from data import process_map_data, process_ts_data

zip_date_ranges, countries_list, source, sources = process_map_data(end_date = ‘2016-10-10’)

markets_list, ts_event, ts_events, ts_source, ts_sources = process_ts_data(end_date = ‘2017-01-01’)

However, when I run the server, I couldn’t quite figure out why these functions get executed twice when I use bokeh server? Here is an example of my log:

2016-10-20 23:01:13,210 Starting Bokeh server version 0.12.3

2016-10-20 23:01:13,215 Starting Bokeh server on port 5006 with applications at paths [’/main’]

2016-10-20 23:01:13,215 Starting Bokeh server with process id: 4867

2016-10-20 23:01:18,788 302 GET / (::1) 0.53ms

Query: WARNING: Loading from cache

Query: INFO: Completed in 0.21 seconds.

Finished processing [map data] …

Query: WARNING: Loading from cache

Query: INFO: Completed in 0.89 seconds.

Finished processing [ts data] …

2016-10-20 23:02:17,145 200 GET /main (::1) 58354.23ms

2016-10-20 23:02:17,146 302 GET / (::1) 0.36ms

Query: WARNING: Loading from cache:

Query: INFO: Completed in 0.22 seconds.

then the sam two functions get executed again !?

Finished processing [map data] …

*Query: WARNING: Loading from cache *

Query: INFO: Completed in 1.03 seconds.

Finished processing [ts data] …

2016-10-20 23:03:17,070 200 GET /demand_index (::1) 59919.84ms

Why do my functions get executed twice? (Happy to provide more code)

Thanks

Did you load the page twice? The script is executed on every new session (i.e. every page load), to create a new document for the session. That is normal and expected behaviour. If you have some expensive operation that only needs to happen once, you can look into lifecycle hooks to control that kind of thing:

  http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#lifecycle-hooks

A concrete example is the spectrogram:

  https://github.com/bokeh/bokeh/tree/master/examples/app/spectrogram

Thanks,

Bryan

···

On Oct 21, 2016, at 1:32 AM, _rchang_ <[email protected]> wrote:

Hi team,

I have a simple bokeh data visualization. The code is organized in the following way:

================================================================

* data.py
    - process_map_data
    - process_ts_data
* main.py

================================================================

process_map_data & process_ts_data are simply helper functions for querying.

def process_map_data(end_date):
    ...
    print "Finished processing [map data] ... "

def process_ts_data(end_date):
    ...
    print "Finished processing [ts data] ... "

In the main.py file, I imported the two functions because they are convenient to use, very much in the same style as the Gapminder example on the official website.

...
from data import process_map_data, process_ts_data
zip_date_ranges, countries_list, source, sources = process_map_data(end_date = '2016-10-10')
markets_list, ts_event, ts_events, ts_source, ts_sources = process_ts_data(end_date = '2017-01-01')
...

However, when I run the server, I couldn't quite figure out why these functions get executed twice when I use bokeh server? Here is an example of my log:

2016-10-20 23:01:13,210 Starting Bokeh server version 0.12.3
2016-10-20 23:01:13,215 Starting Bokeh server on port 5006 with applications at paths ['/main']
2016-10-20 23:01:13,215 Starting Bokeh server with process id: 4867
2016-10-20 23:01:18,788 302 GET / (::1) 0.53ms
Query: WARNING: Loading from cache
Query: INFO: Completed in 0.21 seconds.

Finished processing [map data] ...
Query: WARNING: Loading from cache
Query: INFO: Completed in 0.89 seconds.

Finished processing [ts data] ...
2016-10-20 23:02:17,145 200 GET /main (::1) 58354.23ms
2016-10-20 23:02:17,146 302 GET / (::1) 0.36ms
Query: WARNING: Loading from cache:
Query: INFO: Completed in 0.22 seconds.

then the sam two functions get executed again !?

Finished processing [map data] ...
Query: WARNING: Loading from cache
Query: INFO: Completed in 1.03 seconds.

Finished processing [ts data] ...
2016-10-20 23:03:17,070 200 GET /demand_index (::1) 59919.84ms

Why do my functions get executed twice? (Happy to provide more code)

Thanks

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/a892964a-d552-4ba2-b6cd-982f73b3471d%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Thank you Bryan,

A noob question. How can I use the lifecycle hooks to run a query against my database, retrieve the results and stored as a dataFrame that can be used in my main.py? I understand the general idea of executing function calls on each server load/session load, but I don’t know how to expose the data from server_lifecycle.py to my code in main.py.

Thanks

···

On Friday, October 21, 2016 at 8:16:15 AM UTC-7, Bryan Van de ven wrote:

Did you load the page twice? The script is executed on every new session (i.e. every page load), to create a new document for the session. That is normal and expected behaviour. If you have some expensive operation that only needs to happen once, you can look into lifecycle hooks to control that kind of thing:

    [http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#lifecycle-hooks](http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#lifecycle-hooks)

A concrete example is the spectrogram:

    [https://github.com/bokeh/bokeh/tree/master/examples/app/spectrogram](https://github.com/bokeh/bokeh/tree/master/examples/app/spectrogram)

Thanks,

Bryan

On Oct 21, 2016, at 1:32 AM, rchang [email protected] wrote:

Hi team,

I have a simple bokeh data visualization. The code is organized in the following way:

================================================================

  • data.py
- process_map_data
- process_ts_data
  • main.py

================================================================

process_map_data & process_ts_data are simply helper functions for querying.

def process_map_data(end_date):

...
print "Finished processing [map data] ... "

def process_ts_data(end_date):

...
print "Finished processing [ts data] ... "

In the main.py file, I imported the two functions because they are convenient to use, very much in the same style as the Gapminder example on the official website.

from data import process_map_data, process_ts_data

zip_date_ranges, countries_list, source, sources = process_map_data(end_date = ‘2016-10-10’)

markets_list, ts_event, ts_events, ts_source, ts_sources = process_ts_data(end_date = ‘2017-01-01’)

However, when I run the server, I couldn’t quite figure out why these functions get executed twice when I use bokeh server? Here is an example of my log:

2016-10-20 23:01:13,210 Starting Bokeh server version 0.12.3

2016-10-20 23:01:13,215 Starting Bokeh server on port 5006 with applications at paths [’/main’]

2016-10-20 23:01:13,215 Starting Bokeh server with process id: 4867

2016-10-20 23:01:18,788 302 GET / (::1) 0.53ms

Query: WARNING: Loading from cache

Query: INFO: Completed in 0.21 seconds.

Finished processing [map data] …

Query: WARNING: Loading from cache

Query: INFO: Completed in 0.89 seconds.

Finished processing [ts data] …

2016-10-20 23:02:17,145 200 GET /main (::1) 58354.23ms

2016-10-20 23:02:17,146 302 GET / (::1) 0.36ms

Query: WARNING: Loading from cache:

Query: INFO: Completed in 0.22 seconds.

then the sam two functions get executed again !?

Finished processing [map data] …

Query: WARNING: Loading from cache
Query: INFO: Completed in 1.03 seconds.

Finished processing [ts data] …

2016-10-20 23:03:17,070 200 GET /demand_index (::1) 59919.84ms

Why do my functions get executed twice? (Happy to provide more code)

Thanks


You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/a892964a-d552-4ba2-b6cd-982f73b3471d%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Perhaps this thread is relevant: https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/2FaTAZED6E4

···

On Sunday, November 6, 2016 at 5:48:47 PM UTC-8, rchang wrote:

Thank you Bryan,

A noob question. How can I use the lifecycle hooks to run a query against my database, retrieve the results and stored as a dataFrame that can be used in my main.py? I understand the general idea of executing function calls on each server load/session load, but I don’t know how to expose the data from server_lifecycle.py to my code in main.py.

Thanks

On Friday, October 21, 2016 at 8:16:15 AM UTC-7, Bryan Van de ven wrote:

Did you load the page twice? The script is executed on every new session (i.e. every page load), to create a new document for the session. That is normal and expected behaviour. If you have some expensive operation that only needs to happen once, you can look into lifecycle hooks to control that kind of thing:

    [http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#lifecycle-hooks](http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#lifecycle-hooks)

A concrete example is the spectrogram:

    [https://github.com/bokeh/bokeh/tree/master/examples/app/spectrogram](https://github.com/bokeh/bokeh/tree/master/examples/app/spectrogram)

Thanks,

Bryan

On Oct 21, 2016, at 1:32 AM, rchang [email protected] wrote:

Hi team,

I have a simple bokeh data visualization. The code is organized in the following way:

================================================================

  • data.py
- process_map_data
- process_ts_data
  • main.py

================================================================

process_map_data & process_ts_data are simply helper functions for querying.

def process_map_data(end_date):

...
print "Finished processing [map data] ... "

def process_ts_data(end_date):

...
print "Finished processing [ts data] ... "

In the main.py file, I imported the two functions because they are convenient to use, very much in the same style as the Gapminder example on the official website.

from data import process_map_data, process_ts_data

zip_date_ranges, countries_list, source, sources = process_map_data(end_date = ‘2016-10-10’)

markets_list, ts_event, ts_events, ts_source, ts_sources = process_ts_data(end_date = ‘2017-01-01’)

However, when I run the server, I couldn’t quite figure out why these functions get executed twice when I use bokeh server? Here is an example of my log:

2016-10-20 23:01:13,210 Starting Bokeh server version 0.12.3

2016-10-20 23:01:13,215 Starting Bokeh server on port 5006 with applications at paths [’/main’]

2016-10-20 23:01:13,215 Starting Bokeh server with process id: 4867

2016-10-20 23:01:18,788 302 GET / (::1) 0.53ms

Query: WARNING: Loading from cache

Query: INFO: Completed in 0.21 seconds.

Finished processing [map data] …

Query: WARNING: Loading from cache

Query: INFO: Completed in 0.89 seconds.

Finished processing [ts data] …

2016-10-20 23:02:17,145 200 GET /main (::1) 58354.23ms

2016-10-20 23:02:17,146 302 GET / (::1) 0.36ms

Query: WARNING: Loading from cache:

Query: INFO: Completed in 0.22 seconds.

then the sam two functions get executed again !?

Finished processing [map data] …

Query: WARNING: Loading from cache
Query: INFO: Completed in 1.03 seconds.

Finished processing [ts data] …

2016-10-20 23:03:17,070 200 GET /demand_index (::1) 59919.84ms

Why do my functions get executed twice? (Happy to provide more code)

Thanks


You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/a892964a-d552-4ba2-b6cd-982f73b3471d%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.