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

jl_yield() C API #6006

Closed
stevengj opened this issue Mar 1, 2014 · 3 comments
Closed

jl_yield() C API #6006

stevengj opened this issue Mar 1, 2014 · 3 comments

Comments

@stevengj
Copy link
Member

stevengj commented Mar 1, 2014

It would be nice to have a jl_yield() function in the C API, equivalent to yield() in Julia, so that long-running C programs could give some cycles to the Julia event loop (see e.g. JuliaLang/IJulia.jl#153 or JuliaPy/PyPlot.jl#41 for why this is important even if you just want occasional status updates to be user-visible).

@cbecker mentioned that he tried jl_eval_string("yield()"), but that it crashed Julia upon return. @JeffBezanson, is there something special that needs to be done in order to yield inside of a ccall?

@JeffBezanson
Copy link
Member

jl_eval_string("yield()") should work fine now. I also added jl_yield.

@cbecker
Copy link
Contributor

cbecker commented Mar 2, 2014

Great! I tried it and it works like a charm. The only issue is that the text is only updated every two lines in IJulia, but I suppose that is a different issue related to IJulia itself. An example library call below for future reference
(note that JL_SET_STACK_BASE must not be used here)

#include <stdio.h>
#include <julia.h>

void test()
{
    int i=0;
    for (i=0; i < 10; i++)
    {   
        jl_printf(JL_STDOUT, "hehe\n");
        jl_flush_cstdio();
        jl_yield();

        system("sleep 0.5");
    }
}

To test, compile as library, then ccall() it from julia.

@stevengj
Copy link
Member Author

stevengj commented Mar 3, 2014

Most likely it is grouping the outputs because there are multiple tasks running: the IJulia event loop as well as a task for outputting stdout and another for outputting stderr, for example. When you call jl_yield it only wakes up one of them, so it doesn't wake up the stdout task on every iteration.

In general, I would tend to call jl_yield more often than you print output, instead of always at the same time. You could probably call jl_yield every 10-50ms or so without having any noticable performance impact on your C code.

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