OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-cogserver commits / ed7dbdb

Python: modernize the example usage.
ed7dbdb55a044c6b6bd03de84b48ad6ddc2dfcd2
by Linas Vepstas <linasvepstas@gmail.com> · 2025-12-14T23:43:08.000Z

Changed files (1) +20 -33

examples/python/start_cogserver.py +20 -33
……@@ -4,33 +4,25 @@
44#
55"""
66Example of how the cogserver can be started from python.
7Once the server is started, it can be accessed via it's TCP/IP ports.
8In this example, an AtomSpace will be created and some data poked into
9it. Only afterwards will the cogserver be started. It could also be
10done the other way around: the cogserver could have been started first;
11the order does not matter.
7Once the server is started, it can be accessed via its TCP/IP ports.
8In this example, some Atoms are created in the AtomSpace before the
9cogserver is started. It could also be done the other way around:
10the cogserver could have been started first; the order does not matter.
1211
1312The cogserver will use the same AtomSpace as the python shell; thus,
1413the Atoms created here will be visible there, and vice-versa.
1514
16Once the cogserver is running, The AtomSpace contents are available
17via webspockets in JSON format. The python and scheme evaluators are
18also avialbe through websockets; and all three: python, scheme and json,
15Once the cogserver is running, the AtomSpace contents are available
16via websockets in JSON format. The python and scheme evaluators are
17also available through websockets; and all three: python, scheme and json,
1918are available through telnet. See the html pages in the example
2019directory for more.
2120"""
2221
23# First, import the usual AtomSpace python modules.
24from opencog.atomspace import AtomSpace
2522from opencog.type_constructors import *
23from opencog.cogserver import start_cogserver, stop_cogserver
2624
27# Create an AtomSpace.
28my_atomspace = AtomSpace()
29
30# Tell the type constructors to use it.
31set_thread_atomspace(my_atomspace)
32
33print("\nCreating an AtomSpace and putting some data into it...")
25print("\nCreating some Atoms in the AtomSpace...")
3426A = ConceptNode('Apple')
3527B = ConceptNode('Berry')
3628C = ConceptNode('Comestible')
……@@ -39,18 +31,12 @@ EdgeLink(PredicateNode("is a"), ListLink(A, ConceptNode("fruit")))
3931EdgeLink(PredicateNode("has a"), ListLink(A, ConceptNode("stem")))
4032is_red = EdgeLink(PredicateNode("often colored"), ListLink(A, ConceptNode("red")))
4133
42from opencog.cogserver import (
43 start_cogserver,
44 stop_cogserver,
45 is_cogserver_running,
46 get_server_atomspace
47)
48
4934print("Starting the CogServer ...")
50start_cogserver(atomspace=my_atomspace,
51 console_port=17300, # Default is 17001
52 web_port=18381, # Default is 18080
53 mcp_port=19999) # Default is 18888
35server = start_cogserver(console_port=17300, # Default is 17001
36 web_port=18381, # Default is 18080
37 mcp_port=19999) # Default is 18888
38
39print(f"\nCogServer started: {server}")
5440
5541print("\nYou can connect using:")
5642print(" rlwrap telnet localhost 17300")
……@@ -64,17 +50,18 @@ print("Enter the scm shell with `scm` and then run")
6450print(" (cog-prt-atomspace)")
6551print("Exit the scheme shell with a single dot or a ^D")
6652
67print("\nAlternately, enter the pythonshell with `py` and then run")
68print(" print(\"my atomspace=\", list(my_atomspace))")
53print("\nAlternately, enter the python shell with `py` and then run")
54print(" from opencog.type_constructors import get_thread_atomspace")
55print(" print(list(get_thread_atomspace()))")
6956
7057print("\nYou can create new Atoms in those shells; they will appear here")
71print("because this AtomSpace and the CofserverAtomSpace are one and the same.")
58print("because this AtomSpace and the CogServer AtomSpace are one and the same.")
7259print("If you run MCP, you can even ask Claude to create Atoms for you.")
7360
7461print("\nWill now sleep for ten minutes; after this, the cogserver will stop")
7562import time
7663time.sleep(600)
77stop_cogserver()
78print("... Times up! Goodbye!")
64stop_cogserver(server)
65print("... Time's up! Goodbye!")
7966
8067# THE END. That's All, Folks!