首页 > 解决方案 > 无法启动新线程由 thread.start_new_thread() 抛出

问题描述

thread.start_new_thread()"can't start new thread"尽管用户限制和资源很好,但会抛出。有没有办法知道我为什么会得到can't start new thread?我可以获得有关抛出异常的更多信息吗?我只在我的代码中创建一个线程。

start_new_thread()这是调用函数之前的当前线程和进程数以及用户限制

*** Total number of processes in the system: 304
*** Number of processes owned by user: 2
*** Total number of threads in the system: 452
*** Number of threads owned by user: 9

******************* User limits *********************
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 4133836
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65536
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 102400000
cpu time               (seconds, -t) unlimited
max user processes              (-u) 4133836
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
*****************************************************

这是我的代码

import websocket
import json
import argparse
import subprocess
import logging
import sys
import os
import socket
import signal
import _thread
import time
import re
import pyinotify

############################################################################


def send(message):    
    ws.send(json.dumps(message))

############################################################################

def reportDone():
    message = {'status' : 'DONE'}
    send(message)

############################################################################

def doStartJob(command, args):

    process = subprocess.Popen([command] + args, stdout=out, stderr=err, preexec_fn=os.setpgrp)

    process.wait()
    reportDone()

############################################################################

def startJob(command, args):
    _thread.start_new_thread(doStartJob, (command, args))

############################################################################

def handle_disconnect():
    logging.error("lost connection...")
    killJob(True)
    sys.exit(1)


############################################################################


def on_message(ws, messageStr):
    logging.debug("got message " + messageStr)

    message = json.loads(messageStr)
    command = message.get('cmd')
    if (command == 'START_JOB'):
        startJob(message.get('command'), message.get('args'))
    elif (command == 'KILL_JOB'):
        killJob()
    elif (command == 'EXIT'):
        global lastExitCode
        sys.exit(lastExitCode)

############################################################################

def on_error(ws, error):
    logging.error("web socket error: " + str(error))

############################################################################

def on_close(ws):
    handle_disconnect()

############################################################################

def on_open(ws):
    registerMessage = {'status' : 'REGISTER'}
    send(registerMessage)

############################################################################

def killJob(send_int_signal=False):
    global process
    if (process == None):
        logging.error("Can not kill. Process is None")
        return

    logging.debug("terminating job")


    # sends SIGTERM
    logging.debug("sending SIGTERM")
    os.killpg(process.pid, signal.SIGTERM)



############################################################################
#   Main
############################################################################


# Set exception handler
sys.excepthook = exceptionHandler

# The subprocess
process = None

lastExitCode = 0

url = 'ws://' + args.host + ':' + str(args.port) + '/my_app'

logging.debug("connecting with url: " + url)
ws = websocket.WebSocketApp(url,
                            on_message = on_message,
                            on_error = on_error,
                            on_close = on_close)

ws.on_open = on_open
ws.run_forever()

###########################################################################################

标签: pythonmultithreading

解决方案


推荐阅读