Image for post
Image for post

Following from the Pharo Quick Start guide, we’re going to have a little bit of fun and learn more about what we can do with Pharo.

Let’s extend the Greeter class with another method called ‘counter’. This method will do two things:

  1. Execute a loop 100 times. This loop will increment a counter called ‘count’, pop-up a little display to show the count, and pause for 2 seconds before proceeding.
  2. Create a separate process for executing this loop so that it runs in the background.
| count |
count := 0.
[100 timesRepeat: [count := count + 1.
self inform: count printString.
2 seconds wait]] forkNamed: 'Count de Money'

The message #forkNamed: creates a process with the name that’s passed to it, in our case, “Count de Money”. The message is sent to the block containing the code for our loop.

Run the program by selecting, right-clicking and choosing “Do it” in the Playground for this snippet:

Greeter new counter

Here’s something really cool: if you save and quit Pharo while the program is running, the next time you open Pharo, you’ll see the program continuing from where it left off! This can be very convenient when you’re developing and testing your software.

So give it a try. Save and quit Pharo while the program is running. Do it now.

Open Pharo again. How do you like it? The ability to save the state of the image also comes in handy for simple database applications: data objects in your program can be easily persisted without the need for a database manager.

Now, if you wish, the process can be terminated early before it goes to completion by using the Process Browser, which you open by bringing up the World menu, selecting Tools, and then selecting Process Browser.

In the top-left pane, you can see our process, “Count de Money”. If you right-click on it, you can see a number of options, one of which is Terminate.

Image for post
Image for post

You’ve just learned how to do multiprocessing within a Pharo program! And it was so simple. If you think it’s this simple in Java, Python or JavaScript, think again.

At this point, you should get acquainted with the complete syntax of Pharo (Smalltalk). Follow this interactive tutorial: Learn Smalltalk with Prof Stef.

For further study, refer to this excellent reference: Updated Pharo by Example.

To get acquainted with the Pharo Debugger, I’ve written this tutorial: How to use the Pharo Debugger.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store