Skip to main content
Participant
January 22, 2018
Answered

evalScript taking some time to give back the control to the caller being asynchronous

  • January 22, 2018
  • 2 replies
  • 5186 views

Hi,

I'am calling csInterface.evalScript() which in turns calls a synchronous function inside a c++ library and i logged the time before and after  calling the  csInterface.evalScript() and also on getting the result, following is my observation:

There is a delay of 211 milliseconds before and after calling the evalScript, being asynchronous isn't it supposed to give the control back immediately?My understanding of evalScript was that it should give the control back immediately and give the result whenever it is available but in this case it is happening the other way round, it is taking some time to give the control back along with the result(like a synchronous function).

This topic has been closed for replies.
Correct answer sberic

I'll sum up this discussion in order to answer OP's questions:

athila13898684  wrote

There is a delay of 211 milliseconds before and after calling the evalScript, being asynchronous isn't it supposed to give the control back immediately?

CSInterface.evalScript is not, in fact, asynchronous. It is entirely synchronous and will block for the duration of the ExtendScript processing it triggers.

athila13898684  wrote

My understanding of evalScript was that it should give the control back immediately and give the result whenever it is available but in this case it is happening the other way round, it is taking some time to give the control back along with the result(like a synchronous function).

You are not alone in believing that this is how the function works. What is extra-confusing is that the function blocks but it does not immediately return the response. You still have to handle any returned values in a callback that you provide, which will be added to the normal event loop (message queue) and processed sometime later.

Yes, it is confusing.

athila13898684  wrote

I'am calling csInterface.evalScript() which in turns calls a synchronous function inside a c++ library

There is a delay of 211 milliseconds before and after calling the evalScript

Both the CEP and the ExtendScript contexts process on the Main UI thread. Even if you were to delay the call into your C++ library, if it takes ~200ms to process something you would still cause a hitch in the UI - just later.

It may be possible, however, for you to make your C++ library perform its work asynchronously. There's a promising looking SoServerEval_f function defined in SoCClient.h (amongst others)... Once the work is done in your library (on a separate thread? time sliced somehow?) you might be able to trigger a callback into the ExtendScript context with some of the SoServer (?) APIs and then use the CSXS event system from there to dispatch an event back to CEP...?

2 replies

sberic
sbericCorrect answer
Legend
January 23, 2018

I'll sum up this discussion in order to answer OP's questions:

athila13898684  wrote

There is a delay of 211 milliseconds before and after calling the evalScript, being asynchronous isn't it supposed to give the control back immediately?

CSInterface.evalScript is not, in fact, asynchronous. It is entirely synchronous and will block for the duration of the ExtendScript processing it triggers.

athila13898684  wrote

My understanding of evalScript was that it should give the control back immediately and give the result whenever it is available but in this case it is happening the other way round, it is taking some time to give the control back along with the result(like a synchronous function).

You are not alone in believing that this is how the function works. What is extra-confusing is that the function blocks but it does not immediately return the response. You still have to handle any returned values in a callback that you provide, which will be added to the normal event loop (message queue) and processed sometime later.

Yes, it is confusing.

athila13898684  wrote

I'am calling csInterface.evalScript() which in turns calls a synchronous function inside a c++ library

There is a delay of 211 milliseconds before and after calling the evalScript

Both the CEP and the ExtendScript contexts process on the Main UI thread. Even if you were to delay the call into your C++ library, if it takes ~200ms to process something you would still cause a hitch in the UI - just later.

It may be possible, however, for you to make your C++ library perform its work asynchronously. There's a promising looking SoServerEval_f function defined in SoCClient.h (amongst others)... Once the work is done in your library (on a separate thread? time sliced somehow?) you might be able to trigger a callback into the ExtendScript context with some of the SoServer (?) APIs and then use the CSXS event system from there to dispatch an event back to CEP...?

Trevor:
Legend
January 28, 2018

Bruce Bullis

Just want to confirm this a a bug which is Mac specific. On Windows evalScript is asynchronously.

See Re: Does CSInterface.evalScript() Work Asynchronously in Any Host Application? for the proofs.

It would be nice if it could be fixed.

Jonathan FermanVishakha​ Please take note.

Thanks

Trevor

Bruce Bullis
Community Manager
Community Manager
January 22, 2018

For what purposes do you require less than 211ms response time, from a scripting function in a different application?

sberic
Legend
January 22, 2018

https://forums.adobe.com/people/Bruce+Bullis  wrote

For what purposes do you require less than 211ms response time, from a scripting function in a different application?

Does it matter? Regardless of what the purpose is, Bruce Bullis​, a 211ms pause is going to create a noticeable hitch for the user.

Let's ask this question in a more direct way: the user perception of the evalScript function is that it is asynchronous. This is implicit in the function takes a callback instead of returning a value. However, athila13898684's experience does not seem to support that assumption.

Is evalScript synchronous or asynchronous?

Also, athila13898684 could you provide some (simplified?) code that reproduces what you're seeing?

Bruce Bullis
Community Manager
Community Manager
January 22, 2018

> Does it matter?

Yes, as the caller may have unrealistic expectations around what interactions our apps can support, noticeability aside.

> is evalScript synchronous or asynchronous?


Yes.

ExtendScript is, typically, synchronous, while the environment from which that ExtendScript is being invoked may not be. A panel's JavaScript could be waiting on several asynchronous jobs to complete, all of which may depend on different (synchronous) ExtendScript execution.