Spawns a thread that periodically updates task checkpoint time.
 There is no need to kill this thread as job execution lifetime is the
 same as worker process lifetime.
 @return [Thread] Checkpoint thread
550444
def run_checkpointing
      # task check pointer is scoped to separate class to avoid
      # the secondary thread and main thread modifying the same @task
      # variable (and accidentally clobbering it in the process)
      task_checkpointer = TaskCheckPointer.new(@task.id)
      Thread.new do
        with_thread_name("task:#{@task.id}-checkpoint") do
          while true
            sleep(Config.task_checkpoint_interval)
            task_checkpointer.checkpoint
          end
        end
      end
    end
1061069
def start(self):
        

        if self.run is True:
            self.job = multiprocessing.Process(target=self.indicator)
            self.job.start()
            return self.job
1324725
def schedule(*args, &blk)
      return if blk.nil?
      @stop_mutex.synchronize do
        if @stopped
          GRPC.logger.warn('did not schedule job, already stopped')
          return
        end
        GRPC.logger.info('schedule another job')
        fail 'No worker threads available' if @ready_workers.empty?
        worker_queue = @ready_workers.pop

        fail 'worker already has a task waiting' unless worker_queue.empty?
        worker_queue << [blk, args]
      end
    end
1314637
def spawn_missing_workers_for(queues)
      worker_delta_for(queues).times do |nr|
        spawn_worker!(queues)
        sleep Resque::Pool.spawn_delay if Resque::Pool.spawn_delay
      end
    end
149398
def run
      Thread.current.name = format('rocketjob %03i', id)
      logger.info 'Started'
      until shutdown?
        wait = RocketJob::Config.instance.max_poll_seconds
        if process_available_jobs
          # Keeps workers staggered across the poll interval so that
          # all workers don't poll at the same time
          wait = rand(wait * 1000) / 1000
        end
        break if wait_for_shutdown?(wait)
      end
      logger.info 'Stopping'
    rescue Exception => exc
      logger.fatal('Unhandled exception in job processing thread', exc)
    ensure
      ActiveRecord::Base.clear_active_connections! if defined?(ActiveRecord::Base)
    end
132591
def done?
      return (
        get_jobs_by_status(Executor::STATUS_NONE).size == 0 &&
        get_jobs_by_status(Executor::STATUS_RUNNING).size == 0 &&
        get_jobs_by_status(Executor::STATUS_DONE).size > 0
      )
    end
132598
def _spawn(func, *args, **kwargs):
    
    thr = Thread(target=func, args=args, kwargs=kwargs)
    thr.daemon = True
    thr.start()
    return thr
600169
def _sync(timeout=None):
    
    evt = WeakEvent(auto_reset=False)

    # first ensure that any pending callbacks from worker threads have been delivered
    # these are calls of _fromMain()
    Callback(evt.Signal)
    evt.Wait(timeout=timeout)

    evt.Reset() # reuse

    # grab the current set of inprogress cothreads/events
    wait4 = set(_handlers)
    # because Spawn.Wait() can only be called once, remove them and
    # use 'evt' as a proxy for what I'm waiting on so that overlapping
    # calls to _sync() will wait for these as well.
    # However, this means that our failure will must cascade to subsequent
    # calls to _sync() before we complete.
    _handlers.clear()
    _handlers.add(evt)

    try:
        WaitForAll(wait4, timeout=timeout)
    except Exception as e:
        evt.SignalException(e) # pass along error to next concurrent _sync()
    else:
        evt.Signal()
1329223
def create_processing_thread
      @processing_thread = Thread.new do
        Thread.current.abort_on_exception = true

        loop { break unless processing_thread_tick @stream_connection.read_line }

        @processing_thread = @stream_connection = nil
      end
    end
152616
def spawn_process(self, target, *args):
        
        p = Process(target=target, args=args)
        p.daemon = True
        if target == worker:
            p.daemon = Conf.DAEMONIZE_WORKERS
            p.timer = args[2]
            self.pool.append(p)
        p.start()
        return p
554435
def update_thread
      # Kill thread if it exists
      thread && @thread.kill
      @thread = Thread.new do
        loop do
          message = recv
          @state[message.command] = message.value
          yield(message) if block_given?
        end
      end
    end
1289329
def on_threadpool?
      tp = nil

      @mutex.synchronize do
        return false unless @threadpool # you can't dup nil
        tp = @threadpool.dup
      end

      tp.respond_to?(:include?) and tp.include?(Thread.current)
    end
1358540
def process_queue
      begin
        while blk = Merb::Dispatcher.work_queue.pop
           # we've been blocking on the queue waiting for an item sleeping.
           # when someone pushes an item it wakes up this thread so we 
           # immediately pass execution to the scheduler so we don't 
           # accidentally run this block before the action finishes 
           # it's own processing
          Thread.pass
          blk.call
          break if Merb::Dispatcher.work_queue.empty? && Merb.exiting
        end
      rescue Exception => e
        Merb.logger.warn! %Q!Worker Thread Crashed with Exception:\n#{Merb.exception(e)}\nRestarting Worker Thread!
        retry
      end
    end
1145659
private void mainLoop() {
        for (@AutoreleasePool int i = 0;;) {
            try {
                TimerTask task;
                boolean taskFired;
                synchronized(queue) {
                    // Wait for queue to become non-empty
                    while (queue.isEmpty() && newTasksMayBeScheduled)
                        queue.wait();
                    if (queue.isEmpty())
                        break; // Queue is empty and will forever remain; die

                    // Queue nonempty; look at first evt and do the right thing
                    long currentTime, executionTime;
                    task = queue.getMin();
                    synchronized(task.lock) {
                        if (task.state == TimerTask.CANCELLED) {
                            queue.removeMin();
                            continue;  // No action required, poll queue again
                        }
                        currentTime = System.currentTimeMillis();
                        executionTime = task.nextExecutionTime;
                        if (taskFired = (executionTime<=currentTime)) {
                            if (task.period == 0) { // Non-repeating, remove
                                queue.removeMin();
                                task.state = TimerTask.EXECUTED;
                            } else { // Repeating task, reschedule
                                queue.rescheduleMin(
                                  task.period<0 ? currentTime   - task.period
                                                : executionTime + task.period);
                            }
                        }
                    }
                    if (!taskFired) // Task hasn't yet fired; wait
                        queue.wait(executionTime - currentTime);
                }
                if (taskFired)  // Task fired; run it, holding no locks
                    task.run();
            } catch(InterruptedException e) {
            }
        }
    }
1267485
def flush
      mutex.synchronize do
        if thread && thread.alive?
          queue.push(marker)
          marker.wait(mutex)
        end
      end
    end
448229
def on_worker_exit(worker)
      mutex.synchronize do
        @pool.delete(worker)
        if @pool.empty? && !running?
          stop_event.set
          stopped_event.set
        end
      end
    end
808647
def shift
      updated_queue = []
      found_job = nil
      @queue.each do |job|
        if found_job || job.status == Executor::STATUS_HOLDING
          updated_queue.push(job)
        elsif job.status == Executor::STATUS_NONE
          found_job = job
        end
      end
      @queue = updated_queue
      @time_start = Time.now if @time_start == nil
      return found_job
    end