pynetdicom_legacy icon indicating copy to clipboard operation
pynetdicom_legacy copied to clipboard

Threads aren't dying/closing

Open talbarda opened this issue 9 years ago • 2 comments

Hey, I've implemented a simple "DICOM listener" that listens to incoming DICOM objects (code later) and I've an issue that threads are started to accumulate instead of dying/closing normally.

I saw you implemented an option to put the "MaxAssociationIdleSeconds" parameter in the AE class, but I can't see you already deployed it (did "pip install --upgrade netdicom" and didn't see the change). When are you going to deploy it so it'll be available through "pip"?

As for my code (maybe I'm doing something wrong): author = 'talbarda' from netdicom.applicationentity import AE from netdicom.SOPclass import * from dicom.dataset import Dataset, FileDataset from dicom.UID import ExplicitVRLittleEndian, ImplicitVRLittleEndian, ExplicitVRBigEndian import os

TRANSFER_SYNTAX = [ExplicitVRLittleEndian, ImplicitVRLittleEndian, ExplicitVRBigEndian]

ae_title = 'SOME_AE_TITLE' pacs_port = 9996 STUDIES_PATH='/tmp/studies'

def ensure_dir(f): d = os.path.dirname(f) if not os.path.exists

def _get_study_path(study_id): return os.path.join(STUDIES_PATH, study_id)

def _get_series_path(study_path, series_id): return os.path.join(study_path, series_id)

def _get_dicom_path(DS): series_path = _get_series_path(get_study_path(DS.StudyInstanceUID), DS.SeriesInstanceUID) dicomUID = str(DS.InstanceNumber) + "" + str(DS.SOPInstanceUID) + '.dcm' return os.path.join(series_path, dicomUID)

def get_dataset_meta(): file_meta = Dataset() file_meta.MediaStorageSOPClassUID = '1.2.840.10008.5.1.4.1.1.2' file_meta.MediaStorageSOPInstanceUID = "1.2.3" # !! Need valid UID here file_meta.ImplementationClassUID = "1.2.3.4" # !!! Need valid UIDs here return file_meta

def store_ds(dataset_to_store, sop_instance_uid, study_id): filename = _get_dicom_path(dataset_to_store) ds = FileDataset(filename_or_obj=filename, dataset=dataset_to_store, file_meta=get_dataset_meta(), preamble="\0" * 128) # validate the file's directory exists, and if not create it ensure_dir(filename) ds.save_as(filename)

def on_receive_store(SOPClass, received_dataset): curr_study_id = received_dataset.StudyInstanceUID curr_series_id = received_dataset.SeriesInstanceUID curr_sop_instance_uid = received_dataset.SOPInstanceUID print( "Got message of:\nstudy %r\nseries %r\nsop %r" % (curr_study_id, curr_series_id, curr_sop_instance_uid))

try:
    store_ds(received_dataset, curr_sop_instance_uid, curr_study_id)
    # must return appropriate status
    return SOPClass.Success
except:
    print("Failed storing ds %s" % received_dataset)
    raise  # TODO - return failure?

def on_receive_echo(association): print("Association response received")

def on_associate_response(association): print("Association response received")

def on_associate_request(association): print("Association requested") return True

print("Starting AE %r on port %s" % (ae_title, pacs_port)) MyAE = AE(AET=ae_title, port=pacs_port, SOPSCU=[PatientRootFindSOPClass, PatientRootMoveSOPClass, StudyRootMoveSOPClass, StudyRootFindSOPClass, VerificationSOPClass], SOPSCP=[VerificationSOPClass, StorageSOPClass], SupportedTransferSyntax=TRANSFER_SYNTAX) MyAE.OnAssociateResponse = on_associate_response MyAE.OnAssociateRequest = on_associate_request MyAE.OnReceiveStore = on_receive_store MyAE.OnReceiveEcho = on_receive_echo MyAE.start() MyAE.QuitOnKeyboardInterrupt()

talbarda avatar Jan 09 '16 15:01 talbarda

Just to address the 'when can I install the current github version via pip' element of your question... now!

You just need to install direct from github:

pip install https://github.com/patmun/pynetdicom/zipball/master

edmcdonagh avatar Jan 12 '16 08:01 edmcdonagh

I should add that the version number hasn't been changed, so you can't install this version over the pypi version without uninstalling the default one first. Unless there is some soft of force option.

edmcdonagh avatar Jan 12 '16 08:01 edmcdonagh