Skip to content

Running mutiple COScript and using background threads, anyway serialise them #59

Open

Description

@UXDart

Hello! I'm writing a plugin for Sketch, and inside the cocoa script I use performSelectorInBackground_withObject to start a new thread and don't block the execution of a log running process. that works perfect!
I call coscript.setShouldKeepAround(true); and coscript.setShouldKeepAround(false); when the thread ends. that works ok.

the problem is that, if I start (lets say) 3 scripts, different names, different COScript instances, this happens:

  • thread A works
  • when thread B starts, 'B' takes all work and A stops/waits
  • when C starts and B waits and C starts and finish
  • then B continue and finish
  • then A finish

so for some reason it seems something is making them stop/wait when other thread is working.
I know COScript does not really support threads, but they work... in some way... the question is, why this could be happening? I tried many things, but in all cases this happens. if someone has any tip I can work over that, but I just don't know what else to try... thx!

TIA!

Activity

  1. UXDart commented on May 11, 2018

    @UXDart
    Author

    hmm.. they are sharing the same JSVirtualMachine... so that makes sense then... that must be the reason right? where is COScript creating the JSVirtualMachine? or how can I create a new one for each COScript instance?

    Thanks

  2. UXDart commented on May 11, 2018

    @UXDart
    Author

    found a way to create them with different JSVirtualMachine, so each one has its own Mocha instance, and it happens anyway, threads gets serialised... any tip? TIA!

  3. ccgus commented on May 11, 2018

    @ccgus
    Owner

    Are you creating the CocoaScript instances, or is this something that Sketch is handing off to you?

  4. UXDart commented on May 11, 2018

    @UXDart
    Author

    I tried with Sketch ones, didn't work. so I'm creating the instances myself, so I can do whatever is needed to make it work. atm I find a way to create each instance of Mocha inside COScript using a different JSVirtualMachine, using initWithGlobalContext method. but then nothing works, it seems it can't set a variable there for some reason. I think that is the issue, that Sketch is doing a Mocha.alloc.init so it using the same Mocha global context. it makes sense?

    do you have a sample of how to use the method initWithGlobalContext? what do I need to send exactly so it has a different global context?

    or what do you think may be the cause of the serialising of the threads?

    Thanks!

  5. UXDart commented on May 11, 2018

    @UXDart
    Author

    I'm doing this atm:

    JSContext *contextNew = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]];
    JSGlobalContextRef globalCtx1 = contextNew.JSGlobalContextRef;
    _mochaRuntime = [[Mocha alloc] initWithGlobalContext:globalCtx1 name:nil];

    but that seems to work, but then the setvalue getvalue doesn't work...

  6. ccgus commented on May 11, 2018

    @ccgus
    Owner

    I'm not sure what would be causing it off the top of my head. You might ping the sketch folks.

  7. added a commit that references this issue on Dec 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions