Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextInput stuck when running heavy calculations on server #138

Closed
MHC03 opened this issue Nov 6, 2018 · 9 comments
Closed

TextInput stuck when running heavy calculations on server #138

MHC03 opened this issue Nov 6, 2018 · 9 comments

Comments

@MHC03
Copy link
Contributor

MHC03 commented Nov 6, 2018

When I am trying to run heavy calculations and change the value of the TextInput beforehand, the value change somehow seems to be stuck. I have prepared a small example:

pn.extension()

def heavy_calculation():
    epos_indexed = epos[0:10_000_000]
    cropped_index = np.where((epos_indexed['dx'] >= -5) & (epos_indexed['dx'] <= 10) 
                              & (epos_indexed['dy'] >= -10) & epos_indexed['dy'] <= 15)[0]
    # print(list(cropped_index))
    cropped_epos = epos.iloc[list(cropped_index)]

class Test:
    
    def __init__(self):
        self.button = pn.widgets.Button(name='Click Me!')
        self.txt = pn.widgets.TextInput(name='Misused as Log:')
        self.button.param.watch(self._button_click, 'clicks')
        
    def _button_click(self, clicks):
        self.txt.value = 'Start heavy calculation...'
        heavy_calculation()
        self.txt.value = 'Heavy calculation finished.'
    
    def view(self):
        return pn.Column(self.button, self.txt)

Test().view()

When I run this on a jupyter notebook, there is no problem:

jupyter_heavy_calc

But when I deploy it using panel serve --show, the first message on the TextInput widget does not appear:

server_heavy_calculation

For deploying I have downloaded the notebook as a python file and edited Test().view() to Test().view().server_doc().

epos is just a pandas DataFrame containing 36,000,000 elements.
I am not sure, whether this is a panel or bokeh problem.

@philippjfr
Copy link
Member

philippjfr commented Nov 6, 2018

Thanks for the report. I'll look into but as a quick aside:

For deploying I have downloaded the notebook as a python file and edited Test().show() to Test().show().server_doc().

This shouldn't be necessary, you can have the "app" work both in the notebook and on bokeh server by using Test().show().servable(). That will render in a notebook and work when you use panel serve notebook.ipynb --show.

Also when you want to test bokeh server from a notebook you could do Test().show().show()

@philippjfr
Copy link
Member

Okay, so this is a pretty central part of how bokeh server works, changing a parameter value will schedule a so called next_tick_callback, which won't be processed while the "heavy_calculation" is being processed. Ordinarily you could also schedule the calculation as a next_tick_callback and we could consider exposing that mechanism in an easy way. However it would of course be a lot nicer if we could somehow make the naive approach work, I'll have to read a bit more about tornado and bokeh server to see if that's feasible.

@MHC03
Copy link
Contributor Author

MHC03 commented Nov 9, 2018

Thank you very much for looking into all of the issues. For me personally either way is fine, if it is too hard to make it work the naive way, I can try with the next_tick_callback. (But I have to look into how this works)

@philippjfr
Copy link
Member

@MHC03 So there's probably nothing we can do automatically, however you can do it by using the tornado IOLoop, in your example you'd do something like:

    def _button_click(self, clicks):
        self.txt.value = 'Start heavy calculation...'
        ioloop = tornado.ioloop.IOLoop.current()
        ioloop.add_callback(self._heavy_calculation)
        
    def _heavy_calculation(self):
        heavy_calculation()
        self.txt.value = 'Heavy calculation finished.'

@MHC03
Copy link
Contributor Author

MHC03 commented Nov 15, 2018

For me the above approach does not fully work, the second change to the textinput value does not occur. It is stuck with 'Start heavy calculation...'. If I change the code to following it works in a server but not in a Jupyter Notebook as intended:

def _button_click(self, clicks):
        self.txt.value = 'Start heavy calculation...'
        ioloop = tornado.ioloop.IOLoop.current()
        ioloop.add_callback(heavy_calculation)
        self.txt.value = 'Heavy calculation finished.'

With this I get the expected behaviour in a server, in a Jupyter Notebook it directly jumps to the last line without waiting for the callback to finish. If I have several heavy calculations it gets a bit more complicated through this method.

@jbednar
Copy link
Member

jbednar commented Nov 15, 2018

This code would be a bit less confusing if you changed your show() method to view(), to avoid confusion with Panel's .show() method.

@philippjfr
Copy link
Member

For me the above approach does not fully work, the second change to the textinput value does not occur. It is stuck with 'Start heavy calculation...'.

Yeah, it's not quite clear to me what's going on here, it's definitely not working right in this case but I've used it successfully in other examples.

@MHC03
Copy link
Contributor Author

MHC03 commented Nov 15, 2018

This code would be a bit less confusing if you changed your show() method to view(), to avoid confusion with Panel's .show() method.

Changed it, maybe @philippjfr can also edit his comment.

Yeah, it's not quite clear to me what's going on here, it's definitely not working right in this case but I've used it successfully in other examples.

I will try some workaround with the ioloop callback for now. Thanks a lot for looking into this stuff.

@philippjfr
Copy link
Member

Thanks a lot for looking into this stuff.

No problem, I'm encountering the same issues so this is fairly high priority. Still searching for a better solution, but my knowledge of tornado is lacking a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants