For Python, a non-daemonic thread can be generated by set the property daemon. The following is the introduction of daemon:
A boolean value indicating whether this thread is a daemon thread (True) or not (False). This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.
The entire Python program exits when no alive non-daemon threads are left.
https://docs.python.org/2/library/threading.html#threading.Thread.daemon gives more details.
I know Python and Ruby both have method join() to wait for thread terminates. In addition, in the following code snippet, the program will exit when thread a finishes.
#!/usr/bin/env ruby
# coding: UTF-8
a = Thread.new() do
1000.times do |value|
puts "---" + value.to_s
end
end
while a.status != false
# do something
end
puts 'I am the main thread'
Can Ruby generate non-daemonic threads just like Python?
Ruby Threads exhibit the Python daemonic behaviour by default, but really have no such built in concept.
Your example, without the while (or a join/value) will exit when the main program reaches the end.
For Threads to take the Python non daemonic behaviour, you have to specifically wait for them.
require 'thwait' #stdlib
wait_threads = []
wait_threads.push( Thread.new() do
1000.times do |value|
printf "%s ", value
end
end )
Thread.new() do
sleep 1
puts "I am like a python daemon"
end
ThreadsWait.all_waits( *wait_threads )
puts 'I am the main thread'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With