# Force redraw() / redraw with callback

**URL:** https://discourse.processing.org/t/force-redraw-redraw-with-callback/27246
**Category:** p5py
**Created:** [January 20, 2021, 7:10pm UTC](https://discourse.processing.org/t/force-redraw-redraw-with-callback/27246 "2021-01-20T19:10:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![odotdotdot](https://avatars.discourse-cdn.com/v4/letter/o/c0e974/32.png) [@odotdotdot](https://discourse.processing.org/u/odotdotdot)
#### Post date: [January 20, 2021, 7:10pm UTC](https://discourse.processing.org/t/force-redraw-redraw-with-callback/27246/1 "2021-01-20T19:10:21Z")

</div>

I have a p5py application that requires getting data from the internet on user input. I’m running the sketch with no\_loop() in the setup as animation is only required on user input.

Fetching and processing the data can take a second, but the user input is available immediately and I would like to draw it to the screen right away, then do the fetching and processing.

The following code is inside of key\_pressed(), which keeps track of user input in the object user\_input

```auto
    if key == 'ENTER':
        # assign the global variable text_to_display to whatever the user typed
        text_to_display = user_input
        # call draw to render that text to screen
        redraw()
        # get and process data from the input
        get_and_process(user_input)

```

I would expect redraw to execute before get\_and\_process is called, but that’s not happening. I see the redrawn results only after get\_and\_process is complete.

Maybe something analogous to p5’s request animation frame step is happening. Is there a way for me to force redraw to execute before the program moves on? Or to call get\_and\_process as a callback on redraw()?

---

<div class="post-metadata">

### Author: ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)
#### Post date: [January 20, 2021, 8:46pm UTC](https://discourse.processing.org/t/force-redraw-redraw-with-callback/27246/2 "2021-01-20T20:46:33Z")

</div>

Disclaimer: I know nothing about async techniques and threads in Python.

It seems that the entire `key_pressed()` block must complete before the next frame can redraw. Maybe you could use a thread? Something like –

```auto
from p5 import *
import threading

def setup():
    size(500, 500)
    no_loop()

def draw():
    background(0)
    text(str(frame_count), 10, 10)

def key_pressed(e):
    worker = threading.Thread(target=print_frame_count)
    worker.start()
    redraw()

def print_frame_count():
    time.sleep(2)
    print(frame_count)

run()

```

The frame number is drawn in the display window two seconds before it appears in the console.

---

<div class="post-metadata">

### Author: ![odotdotdot](https://avatars.discourse-cdn.com/v4/letter/o/c0e974/32.png) [@odotdotdot](https://discourse.processing.org/u/odotdotdot)
#### Post date: [January 20, 2021, 9:12pm UTC](https://discourse.processing.org/t/force-redraw-redraw-with-callback/27246/3 "2021-01-20T21:12:32Z")

</div>

That did the trick. Thanks a bunch!
