This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#26 2012-11-18 10:57:25

JH1010
Scratcher
Registered: 2012-05-31
Posts: 1000+

Re: My Python Project

Hardmath123 wrote:

blob8108 wrote:

Hardmath123 wrote:


Well, ideally I would start a new thread from the clock function, which (AFAIK) would not cause the recursion error. I think that's nicer, because functions that go into infinite loops never really appealed to me, probably because of my JS background where you'll freeze the browser.

I don't understand that  tongue  Why does the clock function want to call itself at all? Surely a while loop is what you want...

I didn't get this either in the beginning, but here's the deal: in JS, the browser tries to run all code until it's over, and if it takes long you get the spinning colorful wheel (of death). Try running this:

Code:

while (true) {
}

It freezes.

So what you want to do in JS is to set up a timer which starts a new thread after a slight delay:

Code:

function loop() {
// ...
window.setTimeout(loop,1);
}

This way you keep going up and running the loop every millisecond. Get it?  smile

EDIT: Of course, the modern newfangled way to do it is to use RequestAnimationFrame() which basically asks the browser politely to animate it while the browser gets to be stingy with resources (including pausing/slowing down animations when the tab is blurred).

It's in Python not JavaScript.

Offline

 

#27 2012-11-18 11:23:33

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: My Python Project

Hardmath123 wrote:

So what you want to do in JS is to set up a timer which starts a new thread after a slight delay:

JS is single-threaded.


nXIII

Offline

 

#28 2012-11-18 12:26:54

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: My Python Project

Hardmath123 wrote:

blob8108 wrote:

Hardmath123 wrote:

Well, ideally I would start a new thread from the clock function, which (AFAIK) would not cause the recursion error. I think that's nicer, because functions that go into infinite loops never really appealed to me, probably because of my JS background where you'll freeze the browser.

I don't understand that  tongue  Why does the clock function want to call itself at all? Surely a while loop is what you want...

I didn't get this either in the beginning, but here's the deal: in JS, the browser tries to run all code until it's over, and if it takes long you get the spinning colorful wheel (of death). Try running this:

Code:

while (true) {
}

It freezes.

So what you want to do in JS is to set up a timer which starts a new thread after a slight delay:

Code:

function loop() {
// ...
window.setTimeout(loop,1);
}

This way you keep going up and running the loop every millisecond. Get it?  smile

EDIT: Of course, the modern newfangled way to do it is to use RequestAnimationFrame() which basically asks the browser politely to animate it while the browser gets to be stingy with resources (including pausing/slowing down animations when the tab is blurred).

Yeah, dong what you describe makes sense in Javascript. But as nXIII points out, JS doesn't even have proper threading  tongue

And for a simple Python script that runs in a terminal, I don't think we need to worry about blocking the UI...

Last edited by blob8108 (2012-11-18 12:27:22)


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#29 2012-11-18 14:31:59

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: My Python Project

Hardmath123 wrote:

So what you want to do in JS is to set up a timer which starts a new thread after a slight delay:

Code:

function loop() {
// ...
window.setTimeout(loop,1);
}

This isn't recursive  wink

Offline

 

#30 2012-11-18 14:39:49

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: My Python Project

roijac wrote:

Hardmath123 wrote:

So what you want to do in JS is to set up a timer which starts a new thread after a slight delay:

Code:

function loop() {
// ...
window.setTimeout(loop,1);
}

This isn't recursive  wink

It's a form of tail-call recursion. (The tail call is just delayed.)

Last edited by nXIII (2012-11-18 14:41:56)


nXIII

Offline

 

#31 2012-11-18 20:04:21

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: My Python Project

nXIII wrote:

roijac wrote:

Hardmath123 wrote:

So what you want to do in JS is to set up a timer which starts a new thread after a slight delay:

Code:

function loop() {
// ...
window.setTimeout(loop,1);
}

This isn't recursive  wink

It's a form of tail-call recursion. (The tail call is just delayed.)

Yeah, it's like

when I receive [loop v]
do something
broadcast [loop v]


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#32 2012-11-18 22:18:28

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: My Python Project

Hardmath123 wrote:

nXIII wrote:

roijac wrote:

This isn't recursive  wink

It's a form of tail-call recursion. (The tail call is just delayed.)

Yeah, it's like

when I receive [loop v]
do something
broadcast [loop v]

i dont think so because AFAIK window.setTimeout will just push a function to the event queue and close the stack for this call of the function [ as it reaches the end] so its not like

when I receive [loop v]
do something
broadcast [loop v]
But its more like

There is a call stack that javascript engines [ spidermonkey or v8 etc]  mantain for calling functions [thats why they are so beautifully asynchronous]

so if u call the following 3 callbacks from native code

foo();
foo2();
foo3();

it will just push 3 functions in event queue and execute them accordingly. when the execution for the current events before it in the eQueue has happened.

you can just do

Loop(){
  setTimeout(Loop,1);
}

It will run after the current execution of the stack regardless of how big it is  tongue (that is it may take more then 1 ms after this command was issued ) [ thats why we say never rely on setTimeout & setInterval for scientific accuracy of time its just the lower limit after which the function is to be executed it may be delayed ;-) ]


[qoute=Hardmath123]
So what you want to do in JS is to set up a timer which starts a new thread after a slight delay:
[/qoute]

JavaScript is single threaded bro there is no such thing as a different thread in javascript.

The native funcitons like XHR may use a different thread but all the execution of javascript happens in a single thread. And setTimeout is just a hack with the equeue ;-) nothing big

Last edited by fanofcena (2012-11-18 22:24:12)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#33 2012-11-18 22:35:56

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: My Python Project

fanofcena wrote:

i dont think so because AFAIK window.setTimeout will just push a function to the call stack and close the stack for this call of the function [ as it reaches the end]

There is a call stack that javascript engines [ spidermonkey or v8 etc]  mantain for calling functions [thats why they are so beautifully asynchronous]

Not really. JavaScript engines have a task queue of tasks which are executed serially. When you call setTimeout, it schedules a new task to execute at some time at or after the given interval. Thus the following function: (4 being the specified minimum)

Code:

function f() {
    setTimeout(f, 4);
}

…just schedules itself to execute 4 ms later.

so if u call the following 3 callbacks from native code

foo();
foo2();
foo3();

it will just push 3 functions in call stack and execute them accordingly. when the execution for the current event loop has happened.

Normal function calls in ECMAScript are synchronous, not asynchronous. The current event loop is maintained while the function is executed. It doesn't push any of those functions onto the stack; it invokes foo with zero arguments and ignores the result, invokes foo2 with no arguments and ignores the result, and lastly invokes foo3 with no arguments and ignores the result. If that's in a function, it finishes executing the rest of the function, pops the stack frame, and continues from there until it's finished the current task. Then it moves on to the next task in the event loop (e.g. a timeout or an event firing).

you can just do

Loop(){
  setTimeout(Loop,1);
}

That's not even syntactically valid JavaScript…

EDIT: You edited your post a bit…

EDIT2:

And setTimeout is just a hack with the equeue ;-) nothing big

It's not a hack, it's a language feature.

Last edited by nXIII (2012-11-18 22:43:39)


nXIII

Offline

 

#34 2012-11-18 22:52:40

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: My Python Project

nXIII wrote:

fanofcena wrote:

i dont think so because AFAIK window.setTimeout will just push a function to the call stack and close the stack for this call of the function [ as it reaches the end]

There is a call stack that javascript engines [ spidermonkey or v8 etc]  mantain for calling functions [thats why they are so beautifully asynchronous]

Not really. JavaScript engines have a task queue of tasks which are executed serially. When you call setTimeout, it schedules a new task to execute at some time at or after the given interval. Thus the following function: (4 being the specified minimum)

Code:

function f() {
    setTimeout(f, 4);
}

…just schedules itself to execute 4 ms later.

so if u call the following 3 callbacks from native code

foo();
foo2();
foo3();

it will just push 3 functions in call stack and execute them accordingly. when the execution for the current event loop has happened.

Normal function calls in ECMAScript are synchronous, not asynchronous. The current event loop is maintained while the function is executed. It doesn't push any of those functions onto the stack; it invokes foo with zero arguments and ignores the result, invokes foo2 with no arguments and ignores the result, and lastly invokes foo3 with no arguments and ignores the result. If that's in a function, it finishes executing the rest of the function, pops the stack frame, and continues from there until it's finished the current task. Then it moves on to the next task in the event loop (e.g. a timeout or an event firing).

you can just do

Loop(){
  setTimeout(Loop,1);
}

That's not even syntactically valid JavaScript…

EDIT: You edited your post a bit…

EDIT2:

And setTimeout is just a hack with the equeue ;-) nothing big

It's not a hack, it's a language feature.

Lol its not a language feature , its a browser feature  tongue 

setTimeout is not part of ECMA-262,


http://stackoverflow.com/questions/12335222/settimeout-and-v8


don't confuse browser with the language. javascript core is different browsers provide javascript + bunch_of_w3 specs

Last edited by fanofcena (2012-11-18 22:53:44)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#35 2012-11-18 23:05:27

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: My Python Project

and i mentioned calling them from native code for example you are writing a binding for node.js or via NaCl in C++ that is native code

so it u are calling those 3 functions from native code it will just push them into the event stack and execute them accordingly..


code example ?

Code:

#include<v8.h>
#include<node/node.h>
#include<node/uv.h>
using namspace node;
using namspace v8;


/* Some code omitted here */
Persistent<Function> foo;
void some_Long_Task(uv_req){
   sleep(400); // sleeps for 400 second
}
void callback(uv_req){
          Handle<Value> _Args[3] = { Integer::New(8),String::New("Fooo"),Object::New();
 };
    foo->Call(Context::GetCurrent()->Global(),3,_Argv);
}
Handle<Value> Foo(Arguments &args){
    HandleScope handle; 
    foo = Persistent<Function>::New( Function::New(args[0]));
    uv_work_t * req = new uv_work_t;
    uv_queue_work(uv_default_loop(),req,Long_Task,callback);
    handle.Close(Undefined());
}

void Init(Handle<Object> target) {
Local<FunctionTemplate> Tester = FunctionTemplate::New(Foo);
    
}
NODE_MODULE(foonative, Init)

Now the javascript code to use this

Code:

 foo = require('./foonative.node');
  var foo1 = new foo(function(a,b,c){
     console.log(a,b,c);
  });
 var start = Date.now();
 while(Date.now() - start  > 30000 ){
   /* I am going to make javascript confused */
 };

// the output of the above code will come after 30 seconds cause the awesome while loop wont let it rest ;-)

@Nxii now u get my point ? the native code will just push it back into the equeue of the event stack.

Last edited by fanofcena (2012-11-18 23:12:06)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#36 2012-11-18 23:16:16

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: My Python Project

fanofcena wrote:

Lol its not a language feature , its a browser feature  tongue 

setTimeout is not part of ECMA-262,


http://stackoverflow.com/questions/12335222/settimeout-and-v8


don't confuse browser with the language. javascript core is different browsers provide javascript + bunch_of_w3 specs

I know. I linked to the whatwg html standard that defines it.

I wrote:

Thus the following function: (4 being the specified minimum)

I was simplifying it for the sake of other people  tongue

so it [sic] u [sic] are calling those 3 functions from native code it will just push them into [sic] the event stack and execute them accordingly..

I'm sorry, but when I see the following example:

Code:

foo();
foo2();
foo3();

I do not assume those are three native functions.

EDIT: This is getting seriously off-topic…

Last edited by nXIII (2012-11-18 23:31:01)


nXIII

Offline

 

#37 2012-11-18 23:39:05

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: My Python Project

nXIII wrote:

fanofcena wrote:

Lol its not a language feature , its a browser feature  tongue 

setTimeout is not part of ECMA-262,


http://stackoverflow.com/questions/12335222/settimeout-and-v8


don't confuse browser with the language. javascript core is different browsers provide javascript + bunch_of_w3 specs

I know. I linked to the whatwg html standard that defines it.

I wrote:

Thus the following function: (4 being the specified minimum)

I was simplifying it for the sake of other people  tongue

so it [sic] u [sic] are calling those 3 functions from native code it will just push them into [sic] the event stack and execute them accordingly..

I'm sorry, but when I see the following example:

Code:

foo();
foo2();
foo3();

I do not assume those are three native functions.

EDIT: This is getting seriously off-topic…

It was already a recursively off-topic post  tongue  ,


okay  i explicitely mentioned that assume these 3 functions are called from native code x_x duh
[qoute=fanofcena]
so if u call the following 3 callbacks from native code
[/qoute]

PS ;D everything not ECMA-262 can be called as a browser only hack ;D ;D

return Topic; // with this we shall go back to topic


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#38 2012-11-18 23:41:40

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: My Python Project

fanofcena wrote:

okay  i explicitely mentioned that assume these 3 functions are called from native code x_x duh

fanofcena wrote:

so if u call the following 3 callbacks from native code

Sorry, I assumed you had omitted something.

Last edited by nXIII (2012-11-18 23:43:18)


nXIII

Offline

 

#39 2012-11-18 23:57:48

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: My Python Project

nXIII wrote:

fanofcena wrote:

okay  i explicitely mentioned that assume these 3 functions are called from native code x_x duh

fanofcena wrote:

so if u call the following 3 callbacks from native code

Sorry, I assumed you had omitted something.

It happens :-) , my wording has made even me confused if i said it or not ;-D so u know i speak confusingly ...


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#40 2012-11-19 14:56:18

TheSupremeOverLord
Scratcher
Registered: 2012-09-29
Posts: 100+

Re: My Python Project

Upload it to media fire, or sourceforge.  I ran across this problem when uploading my python and java projects.


http://i1154.photobucket.com/albums/p522/lizzyhipo/MINIPIG.jpg

Offline

 

#41 2012-11-20 02:41:28

JH1010
Scratcher
Registered: 2012-05-31
Posts: 1000+

Re: My Python Project

TheSupremeOverLord wrote:

Upload it to media fire, or sourceforge.  I ran across this problem when uploading my python and java projects.

Your sig says that the image in it is no longer available. Is that deliberate?

Offline

 

#42 2012-11-20 10:26:57

TheSupremeOverLord
Scratcher
Registered: 2012-09-29
Posts: 100+

Re: My Python Project

JH1010 wrote:

TheSupremeOverLord wrote:

Upload it to media fire, or sourceforge.  I ran across this problem when uploading my python and java projects.

Your sig says that the image in it is no longer available. Is that deliberate?

No, I deleted my sig by accident


http://i1154.photobucket.com/albums/p522/lizzyhipo/MINIPIG.jpg

Offline

 

#43 2012-11-20 10:31:59

TheSupremeOverLord
Scratcher
Registered: 2012-09-29
Posts: 100+

Re: My Python Project

To upload application files, run the appcfg.py command with the update action and the name of your application's root directory. The root directory should contain the app.yaml file for the application.

appcfg.py update myapp/
appcfg.py gets the application ID from the app.yaml file, and prompts you for the email address and password of your Google account. After successfully signing in with your account, appcfg.py stores a "cookie" so that it does not need to prompt for a password on subsequent attempts.

You can specify the email address on the command line using the --email option. You cannot specify the password as a command line option.

appcfg.py --email=Albert.Johnson@example.com update myapp/
Only application owners and the developer who uploaded the code can download it. If anyone else attempts to download the app, they'll see an error message like the following:

Fetching file list...
Email: user@example.com
Password for user@example.com:
Error 403: --- begin server output ---
You do not have permission to download this app version.
--- end server output ---


http://i1154.photobucket.com/albums/p522/lizzyhipo/MINIPIG.jpg

Offline

 

#44 2012-11-20 11:11:23

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: My Python Project

Use Github For Gods Sake ..

Its Supposed For Code Sharing!


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

Board footer