pharo-users@lists.pharo.org

Any question about pharo is welcome

View all threads

change process variable while process is running

M
mspgate@gmail.com
Tue, Mar 2, 2021 9:42 AM

Hello everybody, I am kind of new to Pharo so I apologise if my
question is silly :)
how can a change a variable in a process while the process is running?
for example in:
[[ | msg| msg := 'help me'. 100 timesRepeat: [(Delay forSeconds: 0.5)
wait. Transcript show: msg; cr]] fork.

how do I change the value of msg while the process is running in order
to modify what the Transcript is showing?
is that possible?
thanks.
Domenico

Hello everybody, I am kind of new to Pharo so I apologise if my\ question is silly :)\ how can a change a variable in a process while the process is running?\ for example in:\ \[\[ | msg| msg := 'help me'. 100 timesRepeat: \[(Delay forSeconds: 0.5)\ wait. Transcript show: msg; cr\]\] fork.\ \ how do I change the value of msg while the process is running in order\ to modify what the Transcript is showing?\ is that possible?\ thanks.\ Domenico
SV
Sven Van Caekenberghe
Tue, Mar 2, 2021 10:16 AM

Hi,

You might learn something from the class/hierarchy ProcessSpecificVariable, esp. the methods in the category 'process specific' in the Process class, e.g. #psValueAt:[put:]

Sven

On 2 Mar 2021, at 10:42, mspgate@gmail.com wrote:

Hello everybody, I am kind of new to Pharo so I apologise if my
question is silly :)
how can a change a variable in a process while the process is running?
for example in:
[[ | msg| msg := 'help me'. 100 timesRepeat: [(Delay forSeconds: 0.5)
wait. Transcript show: msg; cr]] fork.

how do I change the value of msg while the process is running in order
to modify what the Transcript is showing?
is that possible?
thanks.
Domenico

Hi, You might learn something from the class/hierarchy ProcessSpecificVariable, esp. the methods in the category 'process specific' in the Process class, e.g. #psValueAt:[put:] Sven > On 2 Mar 2021, at 10:42, mspgate@gmail.com wrote: > > Hello everybody, I am kind of new to Pharo so I apologise if my > question is silly :) > how can a change a variable in a process while the process is running? > for example in: > [[ | msg| msg := 'help me'. 100 timesRepeat: [(Delay forSeconds: 0.5) > wait. Transcript show: msg; cr]] fork. > > how do I change the value of msg while the process is running in order > to modify what the Transcript is showing? > is that possible? > thanks. > Domenico >
RO
Richard O'Keefe
Tue, Mar 2, 2021 11:26 AM

How do you modify a variable in another process?
DON'T.
Yes, it is possible.
No, it's an amazingly bad idea in any programming language.

First, let us see a simple, obvious, and portable way to do
what you want.  (It has one minor problem which I'll get to.)

shared := Array new: 1.
shared at: 1 put: initialValue.
[ 1 to: 100 do: [:i |
Transcript show: (shared at: 1); cr
] fork.

The 'shared' object is a secret shared between the new Process and the
(method in the) Process that created it.  Nothing else can see it, let
alone change it.  This is a good thing.

The minor problem I mentioned?
It might not work.  If the two Processes are running on different
cores, and if the compiler is (too) smart (enough), memory writes
from one Process might not be noticed by memory reads at the other.

You should think about processes the way you think about objects.
"How do I change a variable in another object?  DON'T!"
"How do I change a variable in another process? DON'T!"
What variables another object has or another process has are
PRIVATE IMPLEMENTATION DETAILS.
The Object-Oriented rule is

ASK, DON'T TELL.

That is, you should never force an object or process to do
anything, you should ASK it to do something by sending a
message.

The way you send a message to a Process is via a SharedQueue.

shared := SharedQueue new.
[ |text next|
text := 'Kalimera Kosmou'.
1 to: 100 do: [:i |
next := shared nextOrNil.
next ifNotNil: [text := next].
Transcript show: text; cr]
] fork.
shared nextPut: 'Pozdrav svijete'.
shared nextPut: 'Hello world'.

With this approach, the receiving process has complete
and absolute control over when the variable ('text') is
changed and indeed whether it is changed.

There are several other tools you can use, but my experience
has been that getting things right using Processes and
SharedQueues FIRST before trying other kinds of things
(like rolling your own Mailbox or Rendezvous classes) makes
concurrent programming so much easier.

It also drives home that the originating process does not
know when or whether the receiving process has acted on
the message, but that's the case with shared variables too,
modern hardware and operating systems being what they are.
It's just more OBVIOUS this way and more CONTROLLED.

On Tue, 2 Mar 2021 at 22:42, mspgate@gmail.com wrote:

Hello everybody, I am kind of new to Pharo so I apologise if my
question is silly :)
how can a change a variable in a process while the process is running?
for example in:
[[ | msg| msg := 'help me'. 100 timesRepeat: [(Delay forSeconds: 0.5)
wait. Transcript show: msg; cr]] fork.

how do I change the value of msg while the process is running in order
to modify what the Transcript is showing?
is that possible?
thanks.
Domenico

How do you modify a variable in another process? DON'T. Yes, it is possible. No, it's an amazingly bad idea in any programming language. First, let us see a simple, obvious, and portable way to do what you want. (It has one minor problem which I'll get to.) shared := Array new: 1. shared at: 1 put: initialValue. [ 1 to: 100 do: [:i | Transcript show: (shared at: 1); cr ] fork. The 'shared' object is a secret shared between the new Process and the (method in the) Process that created it. Nothing else can see it, let alone change it. This is a good thing. The minor problem I mentioned? It might not work. If the two Processes are running on different cores, and if the compiler is (too) smart (enough), memory writes from one Process might not be noticed by memory reads at the other. You should think about processes the way you think about objects. "How do I change a variable in another object? DON'T!" "How do I change a variable in another process? DON'T!" What variables another object has or another process has are PRIVATE IMPLEMENTATION DETAILS. The Object-Oriented rule is ASK, DON'T TELL. That is, you should never *force* an object or process to do anything, you should ASK it to do something by sending a message. The way you send a message to a Process is via a SharedQueue. shared := SharedQueue new. [ |text next| text := 'Kalimera Kosmou'. 1 to: 100 do: [:i | next := shared nextOrNil. next ifNotNil: [text := next]. Transcript show: text; cr] ] fork. shared nextPut: 'Pozdrav svijete'. shared nextPut: 'Hello world'. With this approach, the receiving process has complete and absolute control over when the variable ('text') is changed and indeed whether it is changed. There are several other tools you can use, but my experience has been that getting things right using Processes and SharedQueues FIRST before trying other kinds of things (like rolling your own Mailbox or Rendezvous classes) makes concurrent programming so much easier. It also drives home that the originating process does not know when or whether the receiving process has acted on the message, but that's the case with shared variables too, modern hardware and operating systems being what they are. It's just more OBVIOUS this way and more CONTROLLED. On Tue, 2 Mar 2021 at 22:42, <mspgate@gmail.com> wrote: > Hello everybody, I am kind of new to Pharo so I apologise if my > question is silly :) > how can a change a variable in a process while the process is running? > for example in: > [[ | msg| msg := 'help me'. 100 timesRepeat: [(Delay forSeconds: 0.5) > wait. Transcript show: msg; cr]] fork. > > how do I change the value of msg while the process is running in order > to modify what the Transcript is showing? > is that possible? > thanks. > Domenico >
M
mspgate@gmail.com
Tue, Mar 2, 2021 11:47 AM

thanks Sven and thanks Richard.

I already thought it was not a good idea to change a variable inside the process as for OOP principles.

but I dint find any solution to do what I want to do, that is a tool for live coding in which the process variables are values for a sequencer sendin OSC messages to another application. I need the process for the timing.

what I had in mind came from the fact that in c++ frameworks to build audio application you can modify variable values inside a thread.

after your answers I think I will have to look for another approach.

thanks Sven and thanks Richard. I already thought it was not a good idea to change a variable inside the process as for OOP principles. but I dint find any solution to do what I want to do, that is a tool for live coding in which the process variables are values for a sequencer sendin OSC messages to another application. I need the process for the timing. what I had in mind came from the fact that in c++ frameworks to build audio application you can modify variable values inside a thread. after your answers I think I will have to look for another approach.
RO
Richard O'Keefe
Wed, Mar 3, 2021 1:34 AM

Why do the parameters for the sequencer have to be
process variables?  Why can't they be instance
variables of an object?

SequenceControl (mutex v1 v2 ...)
initialize
mutex := Mutex new.
v1 := default for v1.
v2 := default for v2.
...
v1
^v1
v1: x
validate x all by itself
mutex critical: [
validate x in context of other variables
v1 := x].

This takes care of all the memory fiddling necessary.
Once again, the existence of this object would be a shared
secret between the process and the creating process.

On Wed, 3 Mar 2021 at 00:48, mspgate@gmail.com wrote:

thanks Sven and thanks Richard.

I already thought it was not a good idea to change a variable inside the
process as for OOP principles.

but I dint find any solution to do what I want to do, that is a tool for
live coding in which the process variables are values for a sequencer
sendin OSC messages to another application. I need the process for the
timing.

what I had in mind came from the fact that in c++ frameworks to build
audio application you can modify variable values inside a thread.

after your answers I think I will have to look for another approach.

Why do the parameters for the sequencer have to be *process* variables? Why can't they be instance variables of an object? SequenceControl (mutex v1 v2 ...) initialize mutex := Mutex new. v1 := default for v1. v2 := default for v2. ... v1 ^v1 v1: x validate x all by itself mutex critical: [ validate x in context of other variables v1 := x]. This takes care of all the memory fiddling necessary. Once again, the existence of this object would be a shared secret between the process and the creating process. On Wed, 3 Mar 2021 at 00:48, <mspgate@gmail.com> wrote: > thanks Sven and thanks Richard. > > I already thought it was not a good idea to change a variable inside the > process as for OOP principles. > > but I dint find any solution to do what I want to do, that is a tool for > live coding in which the process variables are values for a sequencer > sendin OSC messages to another application. I need the process for the > timing. > > what I had in mind came from the fact that in c++ frameworks to build > audio application you can modify variable values inside a thread. > > after your answers I think I will have to look for another approach. >
M
mspgate@gmail.com
Thu, Mar 25, 2021 5:39 PM

this is what I was able to put together thanks to your advice Richard.

https://www.youtube.com/watch?v=NFaR3ZQfOUU&feature=youtu.be

I am further developing this research on Pharo as a tool for live coding. I am sending many OSC messages and they sounds pretty on time if the Process is forked at Processor timingPriority.

is that the maximum priority?

this is what I was able to put together thanks to your advice Richard. https://www.youtube.com/watch?v=NFaR3ZQfOUU&feature=youtu.be I am further developing this research on Pharo as a tool for live coding. I am sending many OSC messages and they sounds pretty on time if the Process is forked at Processor timingPriority. is that the maximum priority?
RO
Richard O'Keefe
Fri, Mar 26, 2021 10:25 AM

Open a Playground in Pharo.
Enter the following text.

(((ProcessorScheduler selectors)
select:  [:each | each endsWith: 'Priority'])
collect: [:each | {Processor perform: each. each}])
sorted:  [:x :y | x first <= y first]

Select it, the ctrl-i or "Inspect It" from a menu).
That gives you a list of the named priorities.

From that you will be able to answer your own question.

On Fri, 26 Mar 2021 at 06:39, mspgate@gmail.com wrote:

this is what I was able to put together thanks to your advice Richard.

https://www.youtube.com/watch?v=NFaR3ZQfOUU&feature=youtu.be

I am further developing this research on Pharo as a tool for live coding.
I am sending many OSC messages and they sounds pretty on time if the
Process is forked at Processor timingPriority.

is that the maximum priority?

Open a Playground in Pharo. Enter the following text. (((ProcessorScheduler selectors) select: [:each | each endsWith: 'Priority']) collect: [:each | {Processor perform: each. each}]) sorted: [:x :y | x first <= y first] Select it, the ctrl-i or "Inspect It" from a menu). That gives you a list of the named priorities. From that you will be able to answer your own question. On Fri, 26 Mar 2021 at 06:39, <mspgate@gmail.com> wrote: > this is what I was able to put together thanks to your advice Richard. > > https://www.youtube.com/watch?v=NFaR3ZQfOUU&feature=youtu.be > > I am further developing this research on Pharo as a tool for live coding. > I am sending many OSC messages and they sounds pretty on time if the > Process is forked at Processor timingPriority. > > is that the maximum priority? >
M
mspgate@gmail.com
Sat, Mar 27, 2021 2:37 PM

thanks! I am now sure I am using the highest priority.

one last question about this, maybe very newbie nut I would like to understand things.

so, in your code example for the Process using the ShareQueue and in my running demo, the ShareQueues are not put between pipes ( | | ) before declaration. I thought it was mandatory to have the pipes to declare variables. what is the meaning of having them declare without pipes?

thanks! I am now sure I am using the highest priority. one last question about this, maybe very newbie nut I would like to understand things. so, in your code example for the Process using the ShareQueue and in my running demo, the ShareQueues are not put between pipes ( | | ) before declaration. I thought it was mandatory to have the pipes to declare variables. what is the meaning of having them declare without pipes?
RO
Richard O'Keefe
Mon, Mar 29, 2021 8:31 AM

Code fragments are by definition incomplete.
SharedQueue with a "d".

On Sun, 28 Mar 2021 at 03:38, mspgate@gmail.com wrote:

thanks! I am now sure I am using the highest priority.

one last question about this, maybe very newbie nut I would like to
understand things.

so, in your code example for the Process using the ShareQueue and in my
running demo, the ShareQueues are not put between pipes ( | | ) before
declaration. I thought it was mandatory to have the pipes to declare
variables. what is the meaning of having them declare without pipes?

Code fragments are by definition incomplete. SharedQueue with a "d". On Sun, 28 Mar 2021 at 03:38, <mspgate@gmail.com> wrote: > thanks! I am now sure I am using the highest priority. > > one last question about this, maybe very newbie nut I would like to > understand things. > > so, in your code example for the Process using the ShareQueue and in my > running demo, the ShareQueues are not put between pipes ( | | ) before > declaration. I thought it was mandatory to have the pipes to declare > variables. what is the meaning of having them declare without pipes? >