Python django.db.connection.close() Examples
The following are 30
code examples of django.db.connection.close().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.db.connection
, or try the search function
.
Example #1
Source File: websockets.py From lexpredict-contraxsuite with GNU Affero General Public License v3.0 | 7 votes |
def _send_to_users(self, qs_users: QuerySet, message_obj: ChannelMessage): """ Send the message to the users returned by the specified Django query set. This is an async method made private for calling it from the sync public method. :param qs_users: Django query set returning User models. Pk field will be requested via values_list(..). :param message_obj: Message to send. :return: """ connected_user_ids = self.get_connected_users() if not connected_user_ids: return # A workaround for "connection already closed" problem. # Looks like this code is being executed in a way that # the "connection" object it accesses is re-used for a long time and appears broken after some long delay. connection.close() layer = get_channel_layer() # type: RedisChannelLayer msg = {'type': 'send_to_client', 'message': message_obj.to_dict()} coros = list() for user_id in qs_users.filter(pk__in=connected_user_ids).values_list('pk', flat=True): send_to_user_coro = layer.group_send(self.user_id_to_group_name(user_id), msg) coros.append(send_to_user_coro) await asyncio.gather(*coros)
Example #2
Source File: db.py From django-htk with MIT License | 6 votes |
def close_connection(): """Closes the connection if we are not in an atomic block. The connection should never be closed if we are in an atomic block, as happens when running tests as part of a django TestCase. Otherwise, closing the connection is important to avoid a connection time out after long actions. Django does not automatically refresh a connection which has been closed due to idleness (this normally happens in the request start/finish part of a webapp's lifecycle, which this process does not have), so we must do it ourselves if the connection goes idle due to stuff taking a really long time. source: http://stackoverflow.com/a/39322632/865091 """ from django.db import connection if not connection.in_atomic_block: connection.close()
Example #3
Source File: orm.py From maas with GNU Affero General Public License v3.0 | 6 votes |
def connected(): """Context manager that ensures we're connected to the database. If there is not yet a connection to the database, this will connect on entry and disconnect on exit. Preexisting connections will be left alone. If the preexisting connection is not usable it is closed and a new connection is made. """ if connection.connection is None: connection.close_if_unusable_or_obsolete() connection.ensure_connection() try: yield finally: connection.close() elif connection.is_usable(): yield else: # Connection is not usable, so we disconnect and reconnect. Since # the connection was previously connected we do not disconnect this # new connection. connection.close_if_unusable_or_obsolete() connection.ensure_connection() yield
Example #4
Source File: orm.py From maas with GNU Affero General Public License v3.0 | 6 votes |
def disable_all_database_connections(): """Replace all connections in this thread with unusable stubs. Specifically, instances of :py:class:`~DisabledDatabaseConnection`. This should help prevent accidental use of the database from the reactor thread. Why? Database access means blocking IO, at least with the connections that Django hands out. While blocking IO isn't forbidden in the reactor thread, it ought to be avoided, because the reactor can't do anything else while it's happening, like handling other IO, or running delayed calls. Django's transaction and connection management code also assumes threads: it associates connections and transactions with the current thread, using threading.local. Using the database from the reactor thread is a recipe for intermingled transactions. """ for alias in connections: connection = connections[alias] if type(connection) is not DisabledDatabaseConnection: connections[alias] = DisabledDatabaseConnection() connection.close()
Example #5
Source File: operation_task.py From Joy_QA_Platform with Apache License 2.0 | 6 votes |
def run_case(base_url, case_id, task_name, task_id): report_id = run_case_by_id(base_url, case_id, task_name,"定时任务",isTask=True) time.sleep(5) # 等待报告信息写入数据库 reports = ReportInfo.objects.all().filter(report_id=report_id) tasks = TaskInfo.objects.filter(id=task_id) if len(tasks) > 0: task = tasks[0] if len(reports) == 0: # 若没有此条报告,则认为用例成功,不再需要后续操作 if len(tasks) > 0: task.fail_times = 0 task.save() else: response_result = get_response_result(report_id) if response_result != True: task.fail_times += 1 task.save() # 存失败记录 failRecord = TaskFailedRecord(task_id=task,report_id=reports[0].id,time=datetime.datetime.fromtimestamp(reports[0].test_time)) failRecord.save() if task.fail_times % 2 == 0 and task.fail_times != 0: receivers = task.receiver_email.split(';') for receiver in receivers: send_warn_mail(task_name, receiver, reports[0].id) connection.close() # 避免造成mysql连接数过多的问题
Example #6
Source File: send_queued_mail.py From django_mail_admin with MIT License | 6 votes |
def handle(self, *args, **options): logger.info('Acquiring lock for sending queued emails at %s.lock' % options['lockfile']) try: with FileLock(options['lockfile']): while 1: try: send_queued(options['processes'], options.get('log_level')) except Exception as e: logger.error(e, exc_info=sys.exc_info(), extra={'status_code': 500}) raise # Close DB connection to avoid multiprocessing errors connection.close() if not OutgoingEmail.objects.filter(status=STATUS.queued) \ .filter(Q(scheduled_time__lte=now()) | Q(scheduled_time=None)).exists(): break except FileLocked: logger.info('Failed to acquire lock, terminating now.')
Example #7
Source File: test_orm.py From maas with GNU Affero General Public License v3.0 | 6 votes |
def test_closes_connections_only_when_leaving_atomic_block(self): # Close the database connection to begin with. connection.close() self.expectThat(connection.connection, Is(None)) @orm.transactional def inner(): # We're inside a `transactional` context here. self.expectThat(connection.connection, Not(Is(None))) return "inner" @orm.transactional def outer(): # We're inside a `transactional` context here too. self.expectThat(connection.connection, Not(Is(None))) # Call `inner`, thus nesting `transactional` contexts. return "outer > " + inner() self.assertEqual("outer > inner", outer()) # The connection has been closed. self.expectThat(connection.connection, Is(None))
Example #8
Source File: operation_task.py From Joy_QA_Platform with Apache License 2.0 | 6 votes |
def get_running_tasks(): global mutex with mutex: result = [] tasks = TaskInfo.objects.filter(is_run=True,is_loop=True) now = datetime.datetime.now() for task in tasks: # 排除可能的重复执行 if task.start_time <= now <= (task.start_time + datetime.timedelta(seconds=5)) and (now - task.last_run_time > datetime.timedelta(seconds=5)): result.append(task) task.last_run_time = now task.save() # if datetime.datetime.now() - task.last_run_time > datetime.timedelta(seconds=task.interval_minute * 60 - 5): # result.append(task) connection.close() if len(result) > 0: for i in result: print("获取到任务:",i.task_name) return result
Example #9
Source File: operation_task.py From Joy_QA_Platform with Apache License 2.0 | 6 votes |
def restart_running_task(): # 清除redis中的任务缓存 cache.delete_pattern("qa_paltform_loop_jobs_*") # 清除redis中的分布式锁,避免偶发的锁出现问题,任务会在执行器中的run_pending阻塞 cache.delete_pattern('*qa_test_platform_get') # 增加是否已经启动了线程的标记,避免每增加一个执行任务就启动一次线程,可能导致任务重复执行 cache.delete_pattern('qa_test_platform_running_flag') print("清除任务缓存、清除锁、清除线程启动标记") start_task_timer = StartTaskTimer(run_task_list, run_job_dict) start_task_timer.start() tasks = TaskInfo.objects.filter(is_run=True, is_loop=True) count = 0 for task in tasks: task.start_time = datetime.datetime.now() + datetime.timedelta(seconds=10*(count+1)) task.save() count = count + 1 connection.close() # 避免造成mysql连接数过多的问题
Example #10
Source File: transfer.py From zulip with Apache License 2.0 | 6 votes |
def transfer_avatars_to_s3(processes: int) -> None: def _transfer_avatar_to_s3(user: UserProfile) -> int: avatar_path = user_avatar_path(user) file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", avatar_path) + ".original" try: with open(file_path, 'rb') as f: s3backend.upload_avatar_image(f, user, user) logging.info("Uploaded avatar for %s in realm %s", user.id, user.realm.name) except FileNotFoundError: pass return 0 users = list(UserProfile.objects.all()) if processes == 1: for user in users: _transfer_avatar_to_s3(user) else: # nocoverage output = [] connection.close() for (status, job) in run_parallel(_transfer_avatar_to_s3, users, processes): output.append(job)
Example #11
Source File: tests.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_ignores_connection_configuration_queries(self): real_ensure_connection = connection.ensure_connection connection.close() def make_configuration_query(): is_opening_connection = connection.connection is None real_ensure_connection() if is_opening_connection: # Avoid infinite recursion. Creating a cursor calls # ensure_connection() which is currently mocked by this method. connection.cursor().execute('SELECT 1' + connection.features.bare_select_suffix) ensure_connection = 'django.db.backends.base.base.BaseDatabaseWrapper.ensure_connection' with mock.patch(ensure_connection, side_effect=make_configuration_query): with self.assertNumQueries(1): list(Car.objects.all())
Example #12
Source File: dbrestore.py From django-dbbackup with BSD 3-Clause "New" or "Revised" License | 6 votes |
def handle(self, *args, **options): """Django command handler.""" self.verbosity = int(options.get('verbosity')) self.quiet = options.get('quiet') self._set_logger_level() try: connection.close() self.filename = options.get('input_filename') self.path = options.get('input_path') self.servername = options.get('servername') self.decrypt = options.get('decrypt') self.uncompress = options.get('uncompress') self.passphrase = options.get('passphrase') self.interactive = options.get('interactive') self.database_name, self.database = self._get_database(options) self.storage = get_storage() self._restore_backup() except StorageError as err: raise CommandError(err)
Example #13
Source File: utils.py From django-dbbackup with BSD 3-Clause "New" or "Revised" License | 6 votes |
def uncompress_file(inputfile, filename): """ Uncompress this file using gzip and change its name. :param inputfile: File to compress :type inputfile: ``file`` like object :param filename: File's name :type filename: ``str`` :returns: Tuple with file and new file's name :rtype: :class:`tempfile.SpooledTemporaryFile`, ``str`` """ zipfile = gzip.GzipFile(fileobj=inputfile, mode="rb") try: outputfile = create_spooled_temporary_file(fileobj=zipfile) finally: zipfile.close() new_basename = os.path.basename(filename).replace('.gz', '') return outputfile, new_basename
Example #14
Source File: utils.py From django-dbbackup with BSD 3-Clause "New" or "Revised" License | 6 votes |
def compress_file(inputfile, filename): """ Compress input file using gzip and change its name. :param inputfile: File to compress :type inputfile: ``file`` like object :param filename: File's name :type filename: ``str`` :returns: Tuple with compressed file and new file's name :rtype: :class:`tempfile.SpooledTemporaryFile`, ``str`` """ outputfile = create_spooled_temporary_file() new_filename = filename + '.gz' zipfile = gzip.GzipFile(filename=filename, fileobj=outputfile, mode="wb") try: inputfile.seek(0) copyfileobj(inputfile, zipfile, settings.TMP_FILE_READ_SIZE) finally: zipfile.close() return outputfile, new_filename
Example #15
Source File: refresh_course_metadata.py From course-discovery with GNU Affero General Public License v3.0 | 6 votes |
def execute_parallel_loader(loader_class, *loader_args): """ ProcessPoolExecutor uses the multiprocessing module. Multiprocessing forks processes, causing connection objects to be copied across processes. The key goal when running multiple Python processes is to prevent any database connections from being shared across processes. Depending on specifics of the driver and OS, the issues that arise here range from non-working connections to socket connections that are used by multiple processes concurrently, leading to broken messaging (e.g., 'MySQL server has gone away'). To get around this, we force each process to open its own connection to the database by closing the existing, copied connection as soon as we're within the new process. This works because as long as there is no existing, open connection, Django is smart enough to initialize a new connection the next time one is necessary. """ connection.close() return execute_loader(loader_class, *loader_args)
Example #16
Source File: utils.py From django-dbbackup with BSD 3-Clause "New" or "Revised" License | 6 votes |
def email_uncaught_exception(func): """ Function decorator for send email with uncaught exceptions to admins. Email is sent to ``settings.DBBACKUP_FAILURE_RECIPIENTS`` (``settings.ADMINS`` if not defined). The message contains a traceback of error. """ @wraps(func) def wrapper(*args, **kwargs): try: func(*args, **kwargs) except Exception: logger = logging.getLogger('dbbackup') exc_type, exc_value, tb = sys.exc_info() tb_str = ''.join(traceback.format_tb(tb)) msg = '%s: %s\n%s' % (exc_type.__name__, exc_value, tb_str) logger.error(msg) raise finally: connection.close() return wrapper
Example #17
Source File: event_poller.py From cito_engine with Apache License 2.0 | 6 votes |
def _get_rabbitmq_messages(self, i): queue_reader = rabbitmq_reader.RabbitMQReader() try: message_frame, message_body = queue_reader.get_message() if not message_frame: raise StopIteration() if not self.parse_message(message_body): logger.error('MsgID:%s could not be written, will retry again.' % message_frame.delivery_tag) else: try: gevent.spawn(queue_reader.delete_message, message_frame) gevent.sleep(0) except Exception as exp: logger.error("Error deleting msg from RabbitMQ: %s" % exp) except StopIteration: pass connection.close()
Example #18
Source File: base.py From django-anonymizer with MIT License | 6 votes |
def run(self, chunksize=2000, parallel=4): self.validate() if not self.replacers: return chunks = self.get_queryset_chunk_iterator(chunksize) if parallel == 0: for objs in chunks: _run(self, objs) else: connection.close() pool = Pool(processes=parallel) futures = [pool.apply_async(_run, (self, objs)) for objs in chunks] for future in futures: future.get() pool.close() pool.join()
Example #19
Source File: event_poller.py From cito_engine with Apache License 2.0 | 6 votes |
def _get_sqs_messages(self): logger.info("-= SQS Poller run: BATCHSIZE=%s, POLLING_INTERVAL=%s =-" % (settings.POLLER_CONFIG['batchsize'], settings.POLLER_CONFIG['interval'])) queue_reader = sqs_reader.SQSReader() for m in queue_reader.get_message_batch(): logger.debug("Received: %s with ID:%s" % (m.get_body(), m.id)) if not self.parse_message(m.get_body()): logger.error('MsgID:%s could not be written, will retry again.' % m.id) else: try: gevent.spawn(queue_reader.delete_message, m) gevent.sleep(0) logger.debug('MsgID:%s deleted from queue' % m.id) except Exception as exp: logger.error("Error deleting msg from SQS: %s" % exp) connection.close()
Example #20
Source File: renderers.py From prospector with GNU General Public License v3.0 | 6 votes |
def render_path(self, *args, **kwargs): # Retry after an InterfaceError max_attempts = 5 for i in range(max_attempts): try: return super().render_path(*args, **kwargs) except InterfaceError as e: self.logger.warning("Caught InterfaceError, closing connection " "and trying again (attempt #%s)", i, exc_info=True) try: connection.close() except: pass self.logger.error("Failed to render page after %s attempts. " "Re-raising last exception...", max_attempts) raise e
Example #21
Source File: transfer.py From zulip with Apache License 2.0 | 6 votes |
def transfer_message_files_to_s3(processes: int) -> None: def _transfer_message_files_to_s3(attachment: Attachment) -> int: file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "files", attachment.path_id) try: with open(file_path, 'rb') as f: guessed_type = guess_type(attachment.file_name)[0] upload_image_to_s3(s3backend.uploads_bucket, attachment.path_id, guessed_type, attachment.owner, f.read()) logging.info("Uploaded message file in path %s", file_path) except FileNotFoundError: # nocoverage pass return 0 attachments = list(Attachment.objects.all()) if processes == 1: for attachment in attachments: _transfer_message_files_to_s3(attachment) else: # nocoverage output = [] connection.close() for status, job in run_parallel(_transfer_message_files_to_s3, attachments, processes): output.append(job)
Example #22
Source File: build_structure_angles.py From protwis with Apache License 2.0 | 5 votes |
def prepare_input(self, proc, items, iteration=1): q = Queue() procs = list() num_items = len(items) num = Value('i', 0) lock = Lock() if not num_items: return False # make sure not to use more jobs than proteins (chunk size will be 0, which is not good) if proc > num_items: proc = num_items chunk_size = int(num_items / proc) connection.close() for i in range(0, proc): first = chunk_size * i if i == proc - 1: last = False else: last = chunk_size * (i + 1) p = Process(target=self.main_func, args=([(first, last), iteration,num,lock])) procs.append(p) p.start() for p in procs: p.join()
Example #23
Source File: test_dblocks.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def test_lock_requires_preexisting_connection(self): connection.close() objid = random_objid() lock = self.make_lock(objid) self.assertRaises( dblocks.DatabaseLockAttemptWithoutConnection, lock.__enter__ )
Example #24
Source File: testcase.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def checkDatabaseUse(self): """Enforce `database_use_permitted`.""" if self.database_use_possible and not self.database_use_permitted: from django.db import connection self.expectThat( connection.connection, testtools.matchers.Is(None), "Test policy forbids use of the database.", ) connection.close()
Example #25
Source File: test_orm.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def test_opens_and_closes_connections(self): for alias in connections: connections[alias].close() for alias in connections: self.assertClosed(alias) with FullyConnected(): for alias in connections: self.assertOpen(alias) for alias in connections: self.assertClosed(alias)
Example #26
Source File: rebuild_all_contact_networks.py From protwis with Apache License 2.0 | 5 votes |
def prepare_input(self, proc, items, iteration=1): q = Queue() procs = list() num_items = len(items) num = Value('i', 0) lock = Lock() if not num_items: return False # make sure not to use more jobs than proteins (chunk size will be 0, which is not good) if proc > num_items: proc = num_items chunk_size = int(num_items / proc) connection.close() for i in range(0, proc): first = chunk_size * i if i == proc - 1: last = False else: last = chunk_size * (i + 1) p = Process(target=self.main_func, args=([(first, last), iteration,num,lock])) procs.append(p) p.start() for p in procs: p.join()
Example #27
Source File: base_build.py From protwis with Apache License 2.0 | 5 votes |
def prepare_input(self, proc, items, iteration=1): q = Queue() procs = list() num_items = len(items) num = Value('i', 0) lock = Lock() if not num_items: return False # make sure not to use more jobs than proteins (chunk size will be 0, which is not good) if proc > num_items: proc = num_items chunk_size = int(num_items / proc) connection.close() for i in range(0, proc): first = chunk_size * i if i == proc - 1: last = False else: last = chunk_size * (i + 1) p = Process(target=self.main_func, args=([(first, last), iteration,num,lock])) procs.append(p) p.start() for p in procs: p.join()
Example #28
Source File: tests.py From django-sqlserver with MIT License | 5 votes |
def test_hooks_cleared_on_reconnect(self): with transaction.atomic(): self.do(1) connection.close() connection.connect() with transaction.atomic(): self.do(2) self.assertDone([2])
Example #29
Source File: repository.py From django-sae with Apache License 2.0 | 5 votes |
def close_connection(e): """ Fix: '2006: MySQL server has gone away', Ref:https://code.djangoproject.com/ticket/21597#comment:29 """ connection.close()
Example #30
Source File: test_orm.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def test_exit_closes_open_connections(self): self.addCleanup(connection.close) connection.ensure_connection() self.assertThat(connection.connection, Not(Is(None))) context = ExclusivelyConnected() context.__exit__() self.assertThat(connection.connection, Is(None))