1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2016 John Spray <john.spray@redhat.com>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 // Python.h comes first because otherwise it clobbers ceph's assert
18 #include "common/debug.h"
21 #include "PyModuleRunner.h"
23 #define dout_context g_ceph_context
24 #define dout_subsys ceph_subsys_mgr
27 std::string handle_pyerror();
29 PyModuleRunner::~PyModuleRunner()
31 Gil gil(pMyThreadState, true);
34 Py_XDECREF(pClassInstance);
35 pClassInstance = nullptr;
42 int PyModuleRunner::serve()
44 assert(pClassInstance != nullptr);
46 // This method is called from a separate OS thread (i.e. a thread not
47 // created by Python), so tell Gil to wrap this in a new thread state.
48 Gil gil(pMyThreadState, true);
50 auto pValue = PyObject_CallMethod(pClassInstance,
51 const_cast<char*>("serve"), nullptr);
57 derr << module_name << ".serve:" << dendl;
58 derr << handle_pyerror() << dendl;
65 void PyModuleRunner::shutdown()
67 assert(pClassInstance != nullptr);
69 Gil gil(pMyThreadState, true);
71 auto pValue = PyObject_CallMethod(pClassInstance,
72 const_cast<char*>("shutdown"), nullptr);
77 derr << "Failed to invoke shutdown() on " << module_name << dendl;
78 derr << handle_pyerror() << dendl;
82 void PyModuleRunner::log(int level, const std::string &record)
85 #define dout_prefix *_dout << "mgr[" << module_name << "] "
86 dout(level) << record << dendl;
88 #define dout_prefix *_dout << "mgr " << __func__ << " "
91 void* PyModuleRunner::PyModuleRunnerThread::entry()
93 // No need to acquire the GIL here; the module does it.
94 dout(4) << "Entering thread for " << mod->get_name() << dendl;