{"id":17,"date":"2018-10-03T15:47:00","date_gmt":"2018-10-03T15:47:00","guid":{"rendered":""},"modified":"2018-12-10T14:09:34","modified_gmt":"2018-12-10T14:09:34","slug":"how-to-use-python-with-hammerdb","status":"publish","type":"post","link":"https:\/\/www.hammerdb.com\/blog\/uncategorized\/how-to-use-python-with-hammerdb\/","title":{"rendered":"How to use Python with HammerDB"},"content":{"rendered":"<div><span style=\"font-family: 'verdana' , sans-serif;\">HammerDB is written in Tcl rather than Python for very specific reasons regarding thread scalability and the reasons are very well explained in this article\u00a0<\/span><a style=\"font-family: Verdana, sans-serif;\" href=\"https:\/\/dzone.com\/articles\/threads-done-right-with-tcl\">Threads Done Right&#8230; With Tcl<\/a>. <span style=\"font-family: 'verdana' , sans-serif;\">Nevertheless a common question is whether it is possible to use Python with HammerDB? and the answer is Yes, in fact it is pretty straightforward to extend HammerDB using a package called tclpython available here\u00a0<a href=\"https:\/\/github.com\/amykyta3\/tclpython\">https:\/\/github.com\/amykyta3\/tclpython<\/a>. The following example is from Red Hat 7.5.\u00a0<\/span><\/div>\n<div><\/div>\n<div><span style=\"font-family: 'verdana' , sans-serif;\">Firstly download\u00a0<\/span><span style=\"font-family: 'verdana' , sans-serif;\">tclpython from github\u00a0<\/span><span style=\"font-family: 'verdana' , sans-serif;\">and extract the files<\/span><\/div>\n<div>\n<pre style=\"font-family: Verdana, sans-serif;\"><span style=\"font-family: 'courier new' , 'courier' , monospace;\">[root@vulture tclpython-master]#\r\nbuild\u00a0 LICENSE\u00a0 Makefile\u00a0 msvc\u00a0 pkg\u00a0 README.md\u00a0 src\u00a0 test\u00a0 VERSION.md<\/span><\/pre>\n<div style=\"font-family: Verdana, sans-serif;\"><span style=\"font-family: verdana, sans-serif; font-size: 1rem;\">Then install python and tcl devel packages. Note that on Red Hat 7.5 this will be Tcl 8.5 however the compiled package will still work with HammerDB that uses Tcl 8.6.<\/span><\/div>\n<\/div>\n<div>\n<pre><span style=\"font-family: 'courier new' , 'courier' , monospace;\">[root@vulture ~]# sudo yum install python-devel tcl-devel<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">Loaded plugins: langpacks, ulninfo<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">Package python-devel-2.7.5-69.0.1.el7_5.x86_64 already installed and latest version<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">...<\/span><\/pre>\n<\/div>\n<div>\n<div><span style=\"font-family: 'verdana' , sans-serif;\">Then compile tclpython.<\/span><\/div>\n<pre><span style=\"font-family: 'courier new' , 'courier' , monospace;\">[root@vulture tclpython-master]# make<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">cc -o build\/tclpython\/tclpython\/tclpython.so.5.0 build\/tclpython\/src\/tclpython.o build\/tclpython\/src\/py.o -shared -s -lpthread -ldl -lutil -lm -lpython2.7 -ltclstub8.5<\/span><\/pre>\n<\/div>\n<div style=\"font-family: Verdana, sans-serif;\">If everything goes right you should see the library in the tclpython directory as follows. Note that this is for Python 2 as by default Python 3 is not installed on Red Hat 7.5 (although this can be installed on Red Hat and tclpython built with Python 3 if you wish).<\/div>\n<div>\n<pre><span style=\"font-family: 'courier new' , 'courier' , monospace;\">\/home\/oracle\/tclpython-master\/build\/tclpython\/tclpython<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">[oracle@vulture tclpython]$ ls<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">pkgIndex.tcl\u00a0 tclpython.so.5.0<\/span><\/pre>\n<div style=\"font-family: Verdana, sans-serif;\">Copy this directory and its contents to the HammerDB lib directory so you have the library and the pkgIndex file.<\/div>\n<div>\n<pre><span style=\"font-family: 'courier new' , 'courier' , monospace;\">[oracle@vulture lib]$ ls tclpython\/<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">pkgIndex.tcl\u00a0 tclpython.so.5.0<\/span><\/pre>\n<div style=\"font-family: Verdana, sans-serif;\"><span style=\"font-size: 1rem;\">You can now create an interpreter with Python and run the example commands<\/span><\/div>\n<\/div>\n<div>\n<pre><span style=\"font-family: 'courier new' , 'courier' , monospace;\">[oracle@vulture HammerDB-3.1]$ .\/hammerdbcli\u00a0<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">HammerDB CLI v3.1<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">Copyright (C) 2003-2018 Steve Shaw<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">Type \"help\" for a list of commands<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">The xml is well-formed, applying configuration<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">hammerdb&gt;package require tclpython<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">5.0<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">hammerdb&gt;set interpreter [python::interp new]<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">python0<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">hammerdb&gt;$interpreter exec {print(\"Hello World\")}<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">Hello World<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">hammerdb&gt;puts [$interpreter eval 3\/2.0]<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">1.5<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">hammerdb&gt;python::interp delete $interpreter<\/span>\r\n<span style=\"font-family: 'courier new' , 'courier' , monospace;\">hammerdb&gt;exit<\/span><\/pre>\n<div style=\"font-family: Verdana, sans-serif;\"><span style=\"font-size: 1rem;\">Using this method you can use Python to add your own functions to the driver scripts or write your own testing scripts. Note that in the HammerDB GUI there is functionality to capture any output written to stdout and direct it to the appropriate virtual user output and therefore any use of print in Python needs to do the same or output will be written to the command line where HammerDB was called.<\/span><\/div>\n<p><script>\n  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\n  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\n  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n  })(window,document,'script','https:\/\/www.google-analytics.com\/analytics.js','ga');\n  ga('create', 'UA-87720879-1', 'auto');\n  ga('send', 'pageview');\n<\/script><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>HammerDB is written in Tcl rather than Python for very specific reasons regarding thread scalability and the reasons are very well explained in this article\u00a0Threads Done Right&#8230; With Tcl. Nevertheless a common question is whether it is possible to use Python with HammerDB? and the answer is Yes, in fact it is pretty straightforward to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.hammerdb.com\/blog\/uncategorized\/how-to-use-python-with-hammerdb\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to use Python with HammerDB&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"ppma_author":[5],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"authors":[{"term_id":5,"user_id":2,"is_guest":0,"slug":"hammerdb","display_name":"HammerDB","avatar_url":{"url":"https:\/\/www.hammerdb.com\/blog\/wp-content\/uploads\/2018\/10\/logo-white.png","url2x":"https:\/\/www.hammerdb.com\/blog\/wp-content\/uploads\/2018\/10\/logo-white.png"},"author_category":"","user_url":"http:\/\/www.hammerdb.com","last_name":"","first_name":"","job_title":"","description":""}],"_links":{"self":[{"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":16,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":292,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions\/292"}],"wp:attachment":[{"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/tags?post=17"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.hammerdb.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}