-
Notifications
You must be signed in to change notification settings - Fork 50
concurrency
Python has many frameworks and libraries for concurrent programming, too many, just take a look at the standard library docs on concurrency: https://docs.python.org/3/library/concurrency.html
Perhaps, the best thing about the Go language, is you do not have to read some huge document like above. You can jump right into concurrent programming with the simple syntax it provides to send and receive data on channels.
The concurrency syntax for the JavaScript backend is similar to the syntax used below for the C++ and other native backends. For more info see: https://github.com/rusthon/Rusthon/wiki/WebWorker-Syntax
Rusthon supports concurrency with channels and <-
syntax inspired by Go. This is supported in the Go, Rust and C++ backends. The Rust backend uses the channel implementation from its standard library. The C++ backend uses cpp-channel by Alex Horn. Note cpp-channel is included in Rusthon's source, and gets built along with your code if you use channels: https://github.com/rusthon/Rusthon/blob/master/pythonjs/runtime/c%2B%2B/cpp-channel.h
send data
sender <- message
receive data
value = <- recver
The builtin function channel
is called with the data type of the channel, it returns two channel objects: the sender and receiver.
rusthon
sender, recver = channel(int)
Note, when using the Rust backend, these two channel objects are distinct types, while in Go and C++ they are actually the same object, because sending and receiving can be done on the same channel. This is just an implementation detail, the only practical difference is that you have more strongly typed channels when using the Rust backend.
rust output
let (sender,recver) = channel::<int>();
go output
sender := make(chan int); recver := sender
c++ output
auto sender = cpp::channel<int>{};
auto recver = sender;
Most often you are going to pass along your channels to other functions, here is the syntax to type function arguments.
sender type
def wrapper( sender: chan Sender<int> ):
above chan Sender<int>
for each backend becomes:
- (c++)
cpp::channel<int>
- (rust)
Receiver<int>
- (go)
chan int
receiver type
def wrapper( recver: chan Receiver<int> ):
above chan Receiver<int>
for each backend becomes:
- (c++)
cpp::channel<int>
- (rust)
Receiver<int>
- (go)
chan int
Spawn a new thread or task depending on the backend.
spawn( my_wrapper(x,y,z) )
spawn
for each backend becomes:
- (c++)
std::thread __thread0__( [&]{my_wrapper(x,y,z);} );
- (rust)
Thread::spawn( move || {my_wrapper(x,y,z);} );
- (go)
go my_wrapper(x,y,z)
TODO: fix the rust and c++ backend channel select
select:
case x = <- A:
y += x
case x = <- B:
y += x
https://github.com/rusthon/Rusthon/blob/master/regtests/go/go_select.py https://github.com/rusthon/Rusthon/blob/master/regtests/rust/rust_select.py