{"id":16451520,"url":"https://github.com/pauldraper/simulator-demo","last_synced_at":"2026-06-15T15:31:15.741Z","repository":{"id":8010361,"uuid":"9418548","full_name":"pauldraper/simulator-demo","owner":"pauldraper","description":"An Internet simulator. This demonstrates how to simulate a multi-threaded paradigm.","archived":false,"fork":false,"pushed_at":"2013-04-14T07:29:14.000Z","size":140,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-20T15:29:05.552Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pauldraper.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-04-13T19:01:24.000Z","updated_at":"2014-04-16T17:47:47.000Z","dependencies_parsed_at":"2022-09-01T10:50:21.031Z","dependency_job_id":null,"html_url":"https://github.com/pauldraper/simulator-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pauldraper/simulator-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldraper%2Fsimulator-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldraper%2Fsimulator-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldraper%2Fsimulator-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldraper%2Fsimulator-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pauldraper","download_url":"https://codeload.github.com/pauldraper/simulator-demo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pauldraper%2Fsimulator-demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34369839,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-15T02:00:07.085Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T10:09:08.175Z","updated_at":"2026-06-15T15:31:15.087Z","avatar_url":"https://github.com/pauldraper.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"simulator-demo\n==============\n\nAn Internet simulator. This demonstrates how to simulate a multi-threaded paradigm.\n\n##Explanation##\n\n`sim.simulator` is the singleton instance of `sim.Simulator` controls the flow of the simulation.\n\nThere are two types of functions: \"instantaneous\" and \"non-instantaneous\". Non-instantaneous\nfunctions can only be called by other non-instantaneous function. All functions may be define as\nnon-instantaneous, if desired.\n\n(1) Instantaneous - Define these functions as you would normally.\n```python\ndef harmonic_mean(*numbers):\n    return len(numbers) / sum(map(lambda x: 1/x, numbers))\n```\n\n(2) Non-instantaneous - Define a generator instead of a function. To make other calls to\nnon-instantaneous function, use `yield`. You may also use `yield` to collect the return value of\nthe call. Return values as normal. (As usual, no return value returns `None`.) **CORRECTION: This\nis a feature of Python 3.3...this will have to be ported to 3.3 to take advantage of return values.\n**\n```python\ndef accept_one_conn(socket):\n    socket1 = yield socket.accept()\n    yield socket1.send('hi')\n    some_bytes = yield socket.receive()\n    socket1.close()\n    return some_bytes\n```\n\nTo have the current \"thread\" sleep, use the non-instantaneous `sim.sleep(timeout)`:\n```python\nfrom sim import simulator, sleep\ndef print_times():\n    for _ in xrange(10):\n        print simulator.scheduler.get_time()\n        yield sleep(1000)\n```\n            \nTo have one thread wait on another (think Java's wait() and notifiy()), use `sim.create_lock()`,\n`sim.wait(lock)`, and `sim.resume(lock)`. For example,\n```python\nfrom sim import create_lock, resume, wait\n\nlock = create_lock()\n\ndef f():\n\tprint 'Waiting'\n\tyield wait(lock)\n\tprint 'Done waiting'\n\ndef g():\n\tprint 'Releasing lock'\n\tyield resume(lock)\n```\n\nA new thread may be spawned from any function by passing `sim.simulator.new_thread()` a\nnon-instantaneous function.  \n```python\nfrom sim import simulator\ndef accept_conns(socket):\n    socket1 = yield socket.accept()\n    simulator.new_thread(handle_conn(socket1))\n\ndef handle_conn(socket):\n    #blah blah\n    yield socket.close()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpauldraper%2Fsimulator-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpauldraper%2Fsimulator-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpauldraper%2Fsimulator-demo/lists"}