Discuss Scratch

Spentine
Scratcher
1000+ posts

Is there multithreading on Scratch?

Would 2 scripts be better than one?

pls dont comment “a” on my profile

also my connect 4 AI
Spentine
Scratcher
1000+ posts

Is there multithreading on Scratch?

If I did
when green flag clicked
code :: grey

would it be faster to do

when green flag clicked
code 1 :: grey

when green flag clicked
code 2 :: grey

pls dont comment “a” on my profile

also my connect 4 AI
Maximouse
Scratcher
1000+ posts

Is there multithreading on Scratch?

Scripts don't run in separate threads, so your examples will be equally fast – in fact, the one with a single script would probably be very slightly faster because it doesn't have to run two scripts every frame.


This is Maximouse's signature. Learn more about signatures.
Jonathan50
Scratcher
1000+ posts

Is there multithreading on Scratch?

Scratch implements multithreading, so at any one instant only one script is really busy, and the others wait. So it can't make your project faster because that would require different processor cores to simultaneously run different scripts. (Thread switches only occur at particular points in scripts/under particular circumstances so as to prevent Scratchers from creating race conditions.)

Not yet a Knight of the Mu Calculus.
SansStudios
Scratcher
1000+ posts

Is there multithreading on Scratch?

If you're interested in concurrency and processing power, I would highly recommend _nix's profile Their newest 5 projects all investigate how scratch handles lag, and how to do tasks faster.

scratchcloud
Click above to check it out! I've worked on it for over a year!























Just like sudden sadness comes hangin' on the breeze
imfh
Scratcher
1000+ posts

Is there multithreading on Scratch?

Depending on what code 1 and code 2 are, and how you run them together in code, either scenario could be faster.

In general, however, the fastest solution involves custom blocks with “run without screen refresh” checked. Running the scripts without screen refresh makes Scratch run the scripts as quickly as it can. If you don't use run without screen refresh, Scratch will often slow things down and limit loops to running 30 times per second.

Note that if you use no screen refresh wrong, it could cause extreme lag. When a no refresh block is running, Scratch doesn't update the screen. If something inside a no refresh block takes a long time to finish, it could severely impact your framerate.

define No Refresh 1
forever // Bad, will cause Scratch to run at 2 fps
code ::grey
end

define No Refresh 2
code ::grey

forever // Better
No Refresh 2
end

Scratch to Pygame converter: https://scratch.mit.edu/discuss/topic/600562/
Spentine
Scratcher
1000+ posts

Is there multithreading on Scratch?

imfh wrote:

Depending on what code 1 and code 2 are, and how you run them together in code, either scenario could be faster.

In general, however, the fastest solution involves custom blocks with “run without screen refresh” checked. Running the scripts without screen refresh makes Scratch run the scripts as quickly as it can. If you don't use run without screen refresh, Scratch will often slow things down and limit loops to running 30 times per second.

Note that if you use no screen refresh wrong, it could cause extreme lag. When a no refresh block is running, Scratch doesn't update the screen. If something inside a no refresh block takes a long time to finish, it could severely impact your framerate.

define No Refresh 1
forever // Bad, will cause Scratch to run at 2 fps
code ::grey
end

define No Refresh 2
code ::grey

forever // Better
No Refresh 2
end
I always do that…

pls dont comment “a” on my profile

also my connect 4 AI
imfh
Scratcher
1000+ posts

Is there multithreading on Scratch?

Spentine wrote:

imfh wrote:

Depending on what code 1 and code 2 are, and how you run them together in code, either scenario could be faster.

In general, however, the fastest solution involves custom blocks with “run without screen refresh” checked. Running the scripts without screen refresh makes Scratch run the scripts as quickly as it can. If you don't use run without screen refresh, Scratch will often slow things down and limit loops to running 30 times per second.

Note that if you use no screen refresh wrong, it could cause extreme lag. When a no refresh block is running, Scratch doesn't update the screen. If something inside a no refresh block takes a long time to finish, it could severely impact your framerate.

-snip-
I always do that…
Then you'll probably be better off running them as a single script, like @Maximouse said.

Last edited by imfh (Jan. 7, 2022 22:49:16)


Scratch to Pygame converter: https://scratch.mit.edu/discuss/topic/600562/
Spentine
Scratcher
1000+ posts

Is there multithreading on Scratch?

imfh wrote:

Spentine wrote:

imfh wrote:

Depending on what code 1 and code 2 are, and how you run them together in code, either scenario could be faster.

In general, however, the fastest solution involves custom blocks with “run without screen refresh” checked. Running the scripts without screen refresh makes Scratch run the scripts as quickly as it can. If you don't use run without screen refresh, Scratch will often slow things down and limit loops to running 30 times per second.

Note that if you use no screen refresh wrong, it could cause extreme lag. When a no refresh block is running, Scratch doesn't update the screen. If something inside a no refresh block takes a long time to finish, it could severely impact your framerate.

-snip-
I always do that…
Then you'll probably be better off running them as a single script, like @Maximouse said.
ok

pls dont comment “a” on my profile

also my connect 4 AI
9pfs
Scratcher
100+ posts

Is there multithreading on Scratch?

You can always use
wait (0) secs
to force Scratch to yield in a script that has refresh disabled.

Update to Chrome 98 immediately More info
Share


topic/571765 MUST be apologized for.
Turns out, it was!!!

Spentine
Scratcher
1000+ posts

Is there multithreading on Scratch?

9pfs wrote:

You can always use
wait (0) secs
to force Scratch to yield in a script that has refresh disabled.
Everybody knows that. Do you even know what multithreading is?

pls dont comment “a” on my profile

also my connect 4 AI
Jonathan50
Scratcher
1000+ posts

Is there multithreading on Scratch?

Spentine wrote:

9pfs wrote:

You can always use
wait (0) secs
to force Scratch to yield in a script that has refresh disabled.
Everybody knows that. Do you even know what multithreading is?
Yes, it yields, so it may cause a thread switch. (And potential race conditions if you share variables between scripts.)

Not yet a Knight of the Mu Calculus.
Zebby121
Scratcher
3 posts

Is there multithreading on Scratch?

Why wouldn't scratch use multithreading on sprite clones
ajskateboarder
Scratcher
1000+ posts

Is there multithreading on Scratch?

Pedantic answer: JavaScript is a single-threaded language, and because Scratch is implemented with JavaScript, it cannot provide true multithreading.

Although, Scratch can emulate multithreading by using callbacks, so running two scripts (I think) just sends each script off into a seperate callback, not a thread. Once each script is in its own callback, Scratch just executes code synchronously statement-by-statement.
Pufferfish_Test
Scratcher
500+ posts

Is there multithreading on Scratch?

ajskateboarder wrote:

Pedantic answer: JavaScript is a single-threaded language, and because Scratch is implemented with JavaScript, it cannot provide true multithreading.
javascript can do multithreading btw

ajskateboarder wrote:

Although, Scratch can emulate multithreading by using callbacks, so running two scripts (I think) just sends each script off into a seperate callback, not a thread. Once each script is in its own callback, Scratch just executes code synchronously statement-by-statement.
That's not really how it works… my understanding is currently running threads are stored in an array, annd the sequencer loops through this array and steps each thread until it yields control to another thread. Callbacks have no part to play in this as far as I can tell, although you are correct in that threads are executed synchronously statement-by-statement.

This is my signature, and appears below eeevvvveeerrrryyy post I write
Try out Ocular



Good evening. I am a gerbil. Are you a gerbil? I know I am. Gerbils are possibly the most important beings in the universe; they are super intelligent and they eat carrots so we don't have to.
If you are reading this, you're probably thinking one of 3 things:
  1. This dude's not a gerbil, he's a pufferfish/human/bison/whatever other organism/inanimate object you mistakenly believe I am.
  2. Why am I reading this????????
  3. I'm hungry
The first one is INCORRECT, and I'm going to have to ask you not to spread that false rumour.
The second one is a valid question, and one that has no satisfactory answer other than that you're really ,really, really bored.
As for the 3rd one - so am I. You're not alone.
Spentine
Scratcher
1000+ posts

Is there multithreading on Scratch?

Alright, so from the answers I heard I will assume multithreading code in Scratch is pointless.

pls dont comment “a” on my profile

also my connect 4 AI
rdococ
Scratcher
500+ posts

Is there multithreading on Scratch?

I find this distinction useful:
Concurrency = two processes running together in some way
Parallelism = two processes running exactly simultaneously

Scratch is concurrent because you can run several scripts at once, but it's not parallel because Scratch runs one script until it yields, then the next script, etc. every tick.

For your example, the one script will be slightly faster because Scratch doesn't need to switch between scripts, but overall it doesn't really matter.
Spentine
Scratcher
1000+ posts

Is there multithreading on Scratch?

Scratch supports concurrency, but not multithreading. While it can run two scripts in parallel, only one processor is running it.

If a script for rendering and another script for computing was running in parallel, Scratch doesn't request 2 CPUs from the user's PC, therefore, only being concurrent and not multithreaded.

I'll close the topic now.

Last edited by Spentine (March 17, 2023 22:05:13)


pls dont comment “a” on my profile

also my connect 4 AI

Powered by DjangoBB